You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
kratos/docs/kratos-protoc.md

1.4 KiB

kratos tool protoc

# generate all
kratos tool protoc api.proto
# generate gRPC
kratos tool protoc --grpc api.proto
# generate BM HTTP
kratos tool protoc --bm api.proto
# generate ecode
kratos tool protoc --ecode api.proto
# generate swagger
kratos tool protoc --swagger api.proto

执行生成如 api.pb.go/api.bm.go/api.swagger.json/api.ecode.go 的对应文件,需要注意的是:ecode生成有固定规则,需要首先是enum类型,且enum名字要以ErrCode结尾,如enum UserErrCode。详情可见:example

该工具在Windows/Linux下运行,需提前安装好 protobuf 工具

kratos tool protoc本质上是拼接好了protoc命令然后进行执行,在执行时会打印出对应执行的protoc命令,如下可见:

protoc --proto_path=$GOPATH --proto_path=$GOPATH/github.com/go-kratos/kratos/third_party --proto_path=. --bm_out=:. api.proto
protoc --proto_path=$GOPATH --proto_path=$GOPATH/github.com/go-kratos/kratos/third_party --proto_path=. --gofast_out=plugins=grpc:. api.proto
protoc --proto_path=$GOPATH --proto_path=$GOPATH/github.com/go-kratos/kratos/third_party --proto_path=. --bswagger_out=:. api.proto
protoc --proto_path=$GOPATH --proto_path=$GOPATH/github.com/go-kratos/kratos/third_party --proto_path=. --ecode_out=:. api.proto