有两种包可以选择:goprotobuf(go官方出品)和gogoprotobuf。gogoprotobuf完全兼容google protobuf,它生成的代码质量和编解码性能均比goprotobuf高一些。
1.安装protoc
首先去 https://github.com/protocolbuffers/protobuf/releases 上下载protobuf的编译器protoc,windows上可以直接下到exe文件(linux则需要编译),最后将下载好的可执行文件拷贝到$GOPATH的bin目录下($GOPATH/bin目录最好添加到系统环境变量里)
2.安装protobuf库文件
go get github.com/golang/protobuf/proto
次库,包含诸如编码(marshaling)、解码(unmarshaling)等功能
3.安装插件
go get github.com/golang/protobuf/protoc-gen-go
此插件被 protoc 使用,用于编译 .proto 文件为 Golang 源文件,通过此源文件可以使用定义在 .proto 文件中的消息。
4.生成go文件
protoc --go_out=. *.proto