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

相关文章
相关标签/搜索