1. 获取go-zero库
go get -u github.com/zeromicro/go-zero
2. 安装goctl
brew install goctl
goctl -v
#goctl version 1.5.4 darwin/amd64
3. 创建.api文件, greet.api
goctl api -o greet.api
syntax = "v1"
info (
title: // TODO: add title
desc: // TODO: add description
author: "peng.chen"
email: "chen_peng7@163.com"
)
type request {
// TODO: add members here and delete this comment
}
type response {
// TODO: add members here and delete this comment
}
service greet-api {
@handler GetUser // TODO: set handler name and delete this comment
get /users/id/:userId(request) returns(response)
@handler CreateUser // TODO: set handler name and delete this comment
post /users/create(request)
}
4. 生成服务端代码
goctl api go -api greet.api -dir greet
结构如下
5.启动服务并运行
go run greet.go -f etc/greet-api.yaml
curl -i http://localhost:8888/users/id/111