npx skills add https://github.com/encoredev/skills --skill encore-go-service在 Encore Go 中,每个包含 API 端点的包会自动成为一个服务。无需特殊配置。
只需创建一个包含至少一个 //encore:api 端点的包:
// user/user.go
package user
import "context"
type User struct {
ID string `json:"id"`
Email string `json:"email"`
Name string `json:"name"`
}
//encore:api public method=GET path=/users/:id
func GetUser(ctx context.Context, params *GetUserParams) (*User, error) {
// 这使得 "user" 成为一个服务
}
user/
├── user.go # API 端点
├── db.go # 数据库(如果需要)
└── migrations/ # SQL 迁移文件
└── 1_create_users.up.sql
最适合新项目 - 从简单开始,需要时再拆分:
my-app/
├── encore.app
├── go.mod
├── api.go # 所有端点
├── db.go # 数据库
└── migrations/
└── 1_initial.up.sql
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
适用于具有清晰领域边界的分布式系统:
my-app/
├── encore.app
├── go.mod
├── user/
│ ├── user.go
│ ├── db.go
│ └── migrations/
├── order/
│ ├── order.go
│ ├── db.go
│ └── migrations/
└── notification/
└── notification.go
将相关服务分组到系统中:
my-app/
├── encore.app
├── go.mod
├── commerce/
│ ├── order/
│ │ └── order.go
│ ├── cart/
│ │ └── cart.go
│ └── payment/
│ └── payment.go
├── identity/
│ ├── user/
│ │ └── user.go
│ └── auth/
│ └── auth.go
└── comms/
├── email/
│ └── email.go
└── push/
└── push.go
只需导入并直接调用函数 - Encore 会处理 RPC:
package order
import (
"context"
"myapp/user" // 导入用户服务
)
//encore:api auth method=GET path=/orders/:id
func GetOrderWithUser(ctx context.Context, params *GetOrderParams) (*OrderWithUser, error) {
order, err := getOrder(ctx, params.ID)
if err != nil {
return nil, err
}
// 这会变成一个 RPC 调用 - Encore 会处理它
orderUser, err := user.GetUser(ctx, &user.GetUserParams{ID: order.UserID})
if err != nil {
return nil, err
}
return &OrderWithUser{Order: order, User: orderUser}, nil
}
在以下情况时拆分:
| 信号 | 操作 |
|---|---|
| 不同的扩展需求 | 拆分(例如,认证与分析) |
| 不同的部署周期 | 拆分 |
| 清晰的领域边界 | 拆分 |
| 共享数据库表 | 保持在一起 |
| 紧密耦合的逻辑 | 保持在一起 |
| 仅为组织代码 | 使用子包,而非服务 |
创建没有 //encore:api 端点的包以共享代码:
my-app/
├── user/
│ └── user.go # 服务(有 API)
├── order/
│ └── order.go # 服务(有 API)
└── internal/
├── util/
│ └── util.go # 不是服务(无 API)
└── validation/
└── validate.go
//encore:api 端点时,它就会成为一个服务每周安装数
123
代码仓库
GitHub 星标数
20
首次出现
2026年1月21日
安全审计
安装于
opencode105
gemini-cli104
codex104
claude-code95
github-copilot86
cursor82
In Encore Go, each package with an API endpoint is automatically a service. No special configuration needed.
Simply create a package with at least one //encore:api endpoint:
// user/user.go
package user
import "context"
type User struct {
ID string `json:"id"`
Email string `json:"email"`
Name string `json:"name"`
}
//encore:api public method=GET path=/users/:id
func GetUser(ctx context.Context, params *GetUserParams) (*User, error) {
// This makes "user" a service
}
user/
├── user.go # API endpoints
├── db.go # Database (if needed)
└── migrations/ # SQL migrations
└── 1_create_users.up.sql
Best for new projects - start simple, split later if needed:
my-app/
├── encore.app
├── go.mod
├── api.go # All endpoints
├── db.go # Database
└── migrations/
└── 1_initial.up.sql
For distributed systems with clear domain boundaries:
my-app/
├── encore.app
├── go.mod
├── user/
│ ├── user.go
│ ├── db.go
│ └── migrations/
├── order/
│ ├── order.go
│ ├── db.go
│ └── migrations/
└── notification/
└── notification.go
Group related services into systems:
my-app/
├── encore.app
├── go.mod
├── commerce/
│ ├── order/
│ │ └── order.go
│ ├── cart/
│ │ └── cart.go
│ └── payment/
│ └── payment.go
├── identity/
│ ├── user/
│ │ └── user.go
│ └── auth/
│ └── auth.go
└── comms/
├── email/
│ └── email.go
└── push/
└── push.go
Just import and call the function directly - Encore handles the RPC:
package order
import (
"context"
"myapp/user" // Import the user service
)
//encore:api auth method=GET path=/orders/:id
func GetOrderWithUser(ctx context.Context, params *GetOrderParams) (*OrderWithUser, error) {
order, err := getOrder(ctx, params.ID)
if err != nil {
return nil, err
}
// This becomes an RPC call - Encore handles it
orderUser, err := user.GetUser(ctx, &user.GetUserParams{ID: order.UserID})
if err != nil {
return nil, err
}
return &OrderWithUser{Order: order, User: orderUser}, nil
}
Split when you have:
| Signal | Action |
|---|---|
| Different scaling needs | Split (e.g., auth vs analytics) |
| Different deployment cycles | Split |
| Clear domain boundaries | Split |
| Shared database tables | Keep together |
| Tightly coupled logic | Keep together |
| Just organizing code | Use sub-packages, not services |
Create packages without //encore:api endpoints for shared code:
my-app/
├── user/
│ └── user.go # Service (has API)
├── order/
│ └── order.go # Service (has API)
└── internal/
├── util/
│ └── util.go # Not a service (no API)
└── validation/
└── validate.go
//encore:api endpointsWeekly Installs
123
Repository
GitHub Stars
20
First Seen
Jan 21, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode105
gemini-cli104
codex104
claude-code95
github-copilot86
cursor82
Lark Skill Maker 教程:基于飞书CLI创建AI技能,自动化工作流与API调用指南
31,500 周安装