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@latestgenerate 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,gostringandequalmethods)
代码在此仓库的目录下,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/gogoprotoThese 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.protoTo 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.protoNote 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
案例
在go/src 目录下,demo/pb/data 下存在一个proto
syntax = "proto3";
// 当其他 proto 文件引用此文件中的消息类型时,需要使用这个命名空间,例如:pb.data.SocMapMessage
package pb.data;
// 控制.pb.go 生产路径,以及 package 名称
option go_package = "pb/data";
message SocMapMessage {
map<string, int64> soc_map = 1;
}go_package
在 Protocol Buffers 中,option go_package = "pb/data"; 指令有以下作用:
- 指定生成的 Go 代码包路径
- 它告诉
protoc编译器生成的 Go 文件应该使用什么包路径 - 这决定了生成的
.pb.go文件中的package声明
- 它告诉
- 影响导入方式
- 当其他 Go 代码导入这个生成的包时,需要使用这个路径
- 例如:
import "pb/data"
- 确定输出位置
- 与
protoc的--go_out和--go_opt=paths=source_relative参数配合使用 - 控制
.pb.go文件的生成位置
- 与
- 影响包名
- 在这个例子中,生成的文件会使用
package data - 包名取自路径的最后一部分 (
data)
- 在这个例子中,生成的文件会使用
package
在 Protocol Buffers 中,package pb.data; 声明有以下作用:
- 命名空间:
- 定义了 proto 文件中所有消息类型的命名空间
- 防止与其他 proto 文件中的同名消息类型发生冲突
- 跨语言影响:
- C++: 转换为 namespace
pb::data - Java: 转换为包名
pb.data - Python: 影响生成的模块结构
- 其他语言: 以类似方式映射到对应语言的命名空间概念
- C++: 转换为 namespace
- 消息引用:
- 当其他 proto 文件引用此文件中的消息类型时,需要使用这个命名空间
- 例如:
pb.data.SocMapMessage
- 与 Go 包名关系:
- 在 Go 中,实际的包名由
go_package选项决定 package声明主要用于 proto 文件之间的引用,而不直接决定 Go 代码的包名
- 在 Go 中,实际的包名由
总之,package pb.data; 定义了 Protocol Buffers 的命名空间,对所有支持的语言都有影响,而 go_package 则专门针对 Go 语言代码生成。
一般使用,cd 到 demo目录下,执行
protoc --go_out=./ --go-grpc_out=./ ./pb/data/soc_map.proto