Hello World
Hello World
Section titled “Hello World”五步构建并运行你的第一个 go-zero HTTP 服务。
第一步:生成服务脚手架
Section titled “第一步:生成服务脚手架”goctl api new greetcd greet第二步:查看 API DSL 文件
Section titled “第二步:查看 API DSL 文件”cat greet.apisyntax = "v1"
type Request { Name string `path:"name,options=you|me"`}
type Response { Message string `json:"message"`}
service greet-api { @handler Greethandler get /from/:name (Request) returns (Response)}第三步:拉取依赖并运行
Section titled “第三步:拉取依赖并运行”go mod tidygo run greet.go -f etc/greet-api.yamlStarting server at 0.0.0.0:8888...第四步:测试
Section titled “第四步:测试”curl http://localhost:8888/from/world# 输出:{"message":""}第五步:添加业务逻辑
Section titled “第五步:添加业务逻辑”打开 internal/logic/greetlogic.go,填写 Greet 函数:
func (l *GreetLogic) Greet(req *types.Request) (resp *types.Response, err error) { return &types.Response{ Message: "Hello, " + req.Name + "!", }, nil}重启服务并重新测试:
curl http://localhost:8888/from/world# 输出:{"message":"Hello, world!"}请求流转过程
Section titled “请求流转过程”HTTP GET /from/world → main.go(注册路由) → handler/greethandler.go(解析请求 → types.Request) → logic/greetlogic.go ← 你的业务代码 → 返回 types.Response(序列化为 JSON)