Golang裏面使用protobuf(proto3)

參考文章:https://developers.google.com/protocol-buffers/docs/gotutorialjava

 

1.下載protoc,地址https://github.com/google/protobuf/releases,裏面能夠找到win/linux/mac的二進制文件, 咱們須要protoc-3.4.0-win32.zip或者protoc-3.4.0-linux-x86_64.zip,把裏面的protoc.exe(其它文件不須要)拷貝到PATH下面linux

 

2.執行指令:c++

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

若是是windows:會下載protoc-gen-go.exe到$GOPATH/bin下,把它拷貝到$PATH,它只是protobuf3的golang插件,還不是真正的編譯器
若是是linux:會下載protoc-gen-go到$GOPATH/bin下,把它拷貝到$PATH,它只是protobuf3的golang插件,還不是真正的編譯器github

 

3.生成c++和golang的代碼(只有golang須要插件,其它語言不須要)golang

測試代碼:https://github.com/wjx0912/GoLangUtils/blob/master/project_protobuf3/proto/addressbook.proto
protoc --go_out . addressbook.proto
protoc --cpp_out . addressbook.proto
protoc --csharp_out . addressbook.proto
protoc --java_out . addressbook.proto
protoc --js_out . addressbook.proto
protoc --objc_out . addressbook.proto
protoc --ruby_out . addressbook.protowindows

 

4.golang的測試代碼:https://github.com/wjx0912/GoLangUtils/tree/master/project_protobuf3

api

5.proto3文檔:https://developers.google.com/protocol-buffers/docs/proto3ruby

go api文檔:https://godoc.org/github.com/golang/protobuf/proto測試

代碼:

https://github.com/google/protobuf/tree/master/examples

相關文章
相關標籤/搜索