encore-go-getting-started

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Getting Started with Encore Go

Encore Go 快速入门

Instructions

操作步骤

Install Encore CLI

安装 Encore CLI

bash
undefined
bash
undefined

macOS

macOS

brew install encoredev/tap/encore
brew install encoredev/tap/encore

Linux/WSL

Linux/WSL

Windows (PowerShell)

Windows (PowerShell)

undefined
undefined

Create a New App

创建新应用

bash
undefined
bash
undefined

Interactive - 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
undefined
encore app create my-app --example=hello-world
undefined

Project 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
id
is your app's unique identifier.
cue
// encore.app
{
    "id": "my-app"
}
该文件标记了你的Encore应用的根目录。
id
是你的应用的唯一标识符。

Create Your First API

创建你的第一个API

In Encore Go, any package with an
//encore:api
endpoint becomes a service:
go
// 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:api
端点的包都会成为一个服务:
go
// 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
undefined
bash
undefined

Start the development server

启动开发服务器

encore run
encore run

Your API is now available at http://localhost:4000

你的API现在可通过 http://localhost:4000 访问

undefined
undefined

Open the Local Dashboard

打开本地控制台

bash
undefined
bash
undefined

Opens the local development dashboard

打开本地开发控制台

encore run
encore run

The dashboard shows:
- All your services and endpoints
- Request/response logs
- Database queries
- Traces and spans

控制台展示以下内容:
- 所有服务和端点
- 请求/响应日志
- 数据库查询记录
- 追踪信息和调用链

Common CLI Commands

常用CLI命令

CommandDescription
encore run
Start the local development server
encore test
Run tests (uses
go test
under the hood)
encore db shell <db>
Open a psql shell to a database
encore gen client
Generate API client code
encore app link
Link to an existing Encore Cloud app
命令描述
encore run
启动本地开发服务器
encore test
运行测试(底层使用
go test
encore db shell <db>
打开数据库的psql命令行界面
encore gen client
生成API客户端代码
encore app link
关联到已有的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
    encore-go-api
    skill)
  • Add authentication (see
    encore-go-auth
    skill)
  • Add infrastructure like Pub/Sub, cron jobs (see
    encore-go-infrastructure
    skill)
  • Deploy to Encore Cloud:
    encore app link
    then
    git push encore
  • 添加更多端点(参考
    encore-go-api
    技能)
  • 添加认证功能(参考
    encore-go-auth
    技能)
  • 添加Pub/Sub、定时任务等基础设施(参考
    encore-go-infrastructure
    技能)
  • 部署到Encore Cloud:先执行
    encore app link
    ,再执行
    git push encore