encore-go-getting-started
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGetting Started with Encore Go
Encore Go 快速入门
Instructions
操作步骤
Install Encore CLI
安装 Encore CLI
bash
undefinedbash
undefinedmacOS
macOS
brew install encoredev/tap/encore
brew install encoredev/tap/encore
Linux/WSL
Linux/WSL
curl -L https://encore.dev/install.sh | bash
curl -L https://encore.dev/install.sh | bash
Windows (PowerShell)
Windows (PowerShell)
iwr https://encore.dev/install.ps1 | iex
undefinediwr https://encore.dev/install.ps1 | iex
undefinedCreate a New App
创建新应用
bash
undefinedbash
undefinedInteractive - choose from templates
交互式 - 从模板中选择
encore app create my-app
encore app create my-app
Or start with a blank Go app
或者从空白Go应用开始
encore app create my-app --example=hello-world
undefinedencore app create my-app --example=hello-world
undefinedProject Structure
项目结构
A minimal Encore Go app:
my-app/
├── encore.app # App configuration
├── go.mod # Go module
└── hello/ # A service (package with API)
└── hello.go # API endpoints一个最简版的Encore Go应用结构:
my-app/
├── encore.app # App配置文件
├── go.mod # Go模块
└── hello/ # 一个服务(包含API的包)
└── hello.go # API端点The encore.app File
encore.app 文件
cue
// encore.app
{
"id": "my-app"
}This file marks the root of your Encore app. The is your app's unique identifier.
idcue
// encore.app
{
"id": "my-app"
}该文件标记了你的Encore应用的根目录。是你的应用的唯一标识符。
idCreate Your First API
创建你的第一个API
In Encore Go, any package with an endpoint becomes a service:
//encore:apigo
// hello/hello.go
package hello
import "context"
type Response struct {
Message string `json:"message"`
}
//encore:api public method=GET path=/hello
func Hello(ctx context.Context) (*Response, error) {
return &Response{Message: "Hello, World!"}, nil
}在Encore Go中,任何包含端点的包都会成为一个服务:
//encore:apigo
// hello/hello.go
package hello
import "context"
type Response struct {
Message string `json:"message"`
}
//encore:api public method=GET path=/hello
func Hello(ctx context.Context) (*Response, error) {
return &Response{Message: "Hello, World!"}, nil
}Run Your App
运行你的应用
bash
undefinedbash
undefinedStart the development server
启动开发服务器
encore run
encore run
Your API is now available at http://localhost:4000
你的API现在可通过 http://localhost:4000 访问
undefinedundefinedOpen the Local Dashboard
打开本地控制台
bash
undefinedbash
undefinedOpens the local development dashboard
打开本地开发控制台
encore run
encore run
Then visit http://localhost:9400
The dashboard shows:
- All your services and endpoints
- Request/response logs
- Database queries
- Traces and spans
控制台展示以下内容:
- 所有服务和端点
- 请求/响应日志
- 数据库查询记录
- 追踪信息和调用链Common CLI Commands
常用CLI命令
| Command | Description |
|---|---|
| Start the local development server |
| Run tests (uses |
| Open a psql shell to a database |
| Generate API client code |
| Link to an existing Encore Cloud app |
| 命令 | 描述 |
|---|---|
| 启动本地开发服务器 |
| 运行测试(底层使用 |
| 打开数据库的psql命令行界面 |
| 生成API客户端代码 |
| 关联到已有的Encore Cloud应用 |
Add Path Parameters
添加路径参数
go
type GetUserParams struct {
ID string
}
type User struct {
ID string `json:"id"`
Name string `json:"name"`
}
//encore:api public method=GET path=/users/:id
func GetUser(ctx context.Context, params *GetUserParams) (*User, error) {
return &User{ID: params.ID, Name: "John"}, nil
}go
type GetUserParams struct {
ID string
}
type User struct {
ID string `json:"id"`
Name string `json:"name"`
}
//encore:api public method=GET path=/users/:id
func GetUser(ctx context.Context, params *GetUserParams) (*User, error) {
return &User{ID: params.ID, Name: "John"}, nil
}Add a Database
添加数据库
go
// db.go
package hello
import "encore.dev/storage/sqldb"
var db = sqldb.NewDatabase("mydb", sqldb.DatabaseConfig{
Migrations: "./migrations",
})Create a migration:
sql
-- hello/migrations/1_create_table.up.sql
CREATE TABLE items (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL
);go
// db.go
package hello
import "encore.dev/storage/sqldb"
var db = sqldb.NewDatabase("mydb", sqldb.DatabaseConfig{
Migrations: "./migrations",
})创建迁移文件:
sql
-- hello/migrations/1_create_table.up.sql
CREATE TABLE items (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL
);Query the Database
查询数据库
go
import "encore.dev/storage/sqldb"
type Item struct {
ID int
Name string
}
func getItem(ctx context.Context, id int) (*Item, error) {
item, err := sqldb.QueryRow[Item](ctx, db, `
SELECT id, name FROM items WHERE id = $1
`, id)
if err != nil {
return nil, err
}
return item, nil
}go
import "encore.dev/storage/sqldb"
type Item struct {
ID int
Name string
}
func getItem(ctx context.Context, id int) (*Item, error) {
item, err := sqldb.QueryRow[Item](ctx, db, `
SELECT id, name FROM items WHERE id = $1
`, id)
if err != nil {
return nil, err
}
return item, nil
}Next Steps
下一步操作
- Add more endpoints (see skill)
encore-go-api - Add authentication (see skill)
encore-go-auth - Add infrastructure like Pub/Sub, cron jobs (see skill)
encore-go-infrastructure - Deploy to Encore Cloud: then
encore app linkgit push encore
- 添加更多端点(参考技能)
encore-go-api - 添加认证功能(参考技能)
encore-go-auth - 添加Pub/Sub、定时任务等基础设施(参考技能)
encore-go-infrastructure - 部署到Encore Cloud:先执行,再执行
encore app linkgit push encore