go使用protobuf


go使用protobuf

在go中使用protobuf,有两个可选用的包goprotobuf(go官方出品)和gogoprotobuf。
gogoprotobuf完全兼容google protobuf,它生成的代码质量和编解码性能均比goprotobuf高一些

mac安装protoc

brew install protobuf

安装protobuf

go get github.com/golang/protobuf/proto

使用goprotobuf

安装

go get github.com/golang/protobuf/protoc-gen-go

使用

protoc --go_out=. *.proto

使用gogoprotobuf

安装gogoprotobuf库

go get github.com/gogo/protobuf/proto
go get github.com/gogo/protobuf/gogoproto //这个不装也没关系

有两个插件可用

protoc-gen-gogo:和protoc-gen-go生成的文件差不多,性能稍微好一点
protoc-gen-gofast:生成的文件更复杂,性能快5-7倍

安装使用 protoc-gen-gogo
  1. go get github.com/gogo/protobuf/protoc-gen-gogo
  2. 进入到库的根目录【比如:cd $GOPATH/pkg/mod/github.com/gogo/protobuf@v1.3.2/protoc-gen-gogo】
  3. 编译生成protoc-gen-gogo 【命令:go build】,并把生成的protoc-gen-gogo文件移到 $GOPATH/bin 目录下方便后续使用
  4. 使用:protoc --gogo_out=. *.proto

注意:此处如果不走第2,3步,使用时可能会报错:protoc-gen-gogo: program not found or is not executable

安装使用 protoc-gen-gofast
  1. go get github.com/gogo/protobuf/protoc-gen-gofast
  2. 进入到库的根目录【比如:cd $GOPATH/pkg/mod/github.com/gogo/protobuf@v1.3.2/protoc-gen-gofast】
  3. 编译生成protoc-gen-gofast 【命令:go build】,并把生成的protoc-gen-gofast 文件移到 $GOPATH/bin 目录下方便后续使用
  4. 使用:protoc --gofast_out=. *.proto

注意:此处如果不走第2,3步,使用时可能会报错:protoc-gen-gofast: program not found or is not executable


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM