protocol
官方
官方仓库:https://pkg.go.dev/google.golang.org/protobuf
This module (github.com/golang/protobuf
) contains Go bindings for protocol buffers.
It has been superseded by the google.golang.org/protobuf
module, which contains an updated and simplified API, support for protobuf reflection, and many other improvements. We recommend that new code use the google.golang.org/protobuf
module.
推荐使用google.golang.org/protobuf
install protoc-gen-go
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
generate pb file
protoc -I=$SRC_DIR --go_out=$DST_DIR $SRC_DIR/addressbook.proto
[Deprecated]gogoprotobuf
仓库:https://github.com/gogo/protobuf
gogoprotobuf is a fork of golang/protobuf with extra code generation features.
// Install the protoc-gen-gofast binary
go install github.com/gogo/protobuf/protoc-gen-gofast
// generate pb file
protoc --gofast_out=. myproto.proto
其他工具,More Speed and more generated code
Fields without pointers cause less time in the garbage collector. More code generation results in more convenient methods.
Other binaries are also included:
- protoc-gen-gogofast (same as gofast, but imports
gogoprotobuf
) - protoc-gen-gogofaster (same as gogofast, without
XXX_unrecognized
, less pointer fields) - protoc-gen-gogoslick (same as gogofaster, but with generated
string
,gostring
andequal
methods)
代码在此仓库的目录下,Installing any of these binaries is easy. Simply run:
go install github.com/gogo/protobuf/proto
go install github.com/gogo/protobuf/{binary} // protoc-gen-gogofast、protoc-gen-gogofaster、protoc-gen-gogoslick
go install github.com/gogo/protobuf/gogoproto
These binaries allow you to use gogoprotobuf extensions. You can also use your own binary.
To generate the code, you also need to set the include path properly.
protoc -I=. -I=$GOPATH/src -I=$GOPATH/src/github.com/gogo/protobuf/protobuf --{binary}_out=. myproto.proto
To use proto files from "google/protobuf" you need to add additional args to protoc.
protoc -I=. -I=$GOPATH/src -I=$GOPATH/src/github.com/gogo/protobuf/protobuf --{binary}_out=\
Mgoogle/protobuf/any.proto=github.com/gogo/protobuf/types,\
Mgoogle/protobuf/duration.proto=github.com/gogo/protobuf/types,\
Mgoogle/protobuf/struct.proto=github.com/gogo/protobuf/types,\
Mgoogle/protobuf/timestamp.proto=github.com/gogo/protobuf/types,\
Mgoogle/protobuf/wrappers.proto=github.com/gogo/protobuf/types:. \
myproto.proto
Note that in the protoc command, {binary} does not contain the initial prefix of "protoc-gen".
Most Speed
Install protoc-gen-gogo:
go install github.com/gogo/protobuf/proto
go install github.com/gogo/protobuf/jsonpb
go install github.com/gogo/protobuf/protoc-gen-gogo
go install github.com/gogo/protobuf/gogoproto
实际区分
案例:https://github.com/xyz3282836/grpcdemo
官方 protoc-gen-go
protoc --go_out=/Users/zhou/go/src/grpcdemo/ api/v1/hello.proto
- 注释:Code generated by protoc-gen-go.
- import:proto "github.com/
golang
/protobuf/proto"
gofast
protoc --gofast_out=plugins=grpc:/Users/zhou/go/src/grpcdemo/ api/v1/hello.proto
注释:Code generated by protoc-gen-gogo.
import:proto "github.com/
golang
/protobuf/proto"
gogofast
protoc --gogofast_out=plugins=grpc:/Users/zhou/go/src/grpcdemo/ api/v1/hello.proto
注释:Code generated by protoc-gen-gogo.
import:proto "github.com/
gogo
/protobuf/proto"
和 gofast 只有 import proto 包不同
gogofaster
protoc --gogofaster_out=plugins=grpc:/Users/zhou/go/src/grpcdemo/ api/v1/hello.proto
注释:Code generated by protoc-gen-gogo.
import:proto "github.com/
gogo
/protobuf/proto"和 gofast,gogofast 比,没有 XXX_NoUnkeyedLiteral,XXX_unrecognized,XXX_sizecache
gogoslick
protoc --gogoslick_out=plugins=grpc:/Users/zhou/go/src/grpcdemo/ api/v1/hello.proto
注释:Code generated by protoc-gen-gogo.
import:proto "github.com/
gogo
/protobuf/proto",reflect "reflect"和 gofast,gogofast 比,没有 XXX_NoUnkeyedLiteral,XXX_unrecognized,XXX_sizecache
生成 string, gostring and equal methods