go install google.golang.org/protobuf/cmd/protoc-gen-go@latest go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest 确保将 $GOPATH/bin 添加到你的 PATH 环境变量中: export PATH=$PATH:$(go env GOPATH)/bin 2. 创建 .proto 文件 创建一个名为 example.proto 的文件,用于定义消息...
messagePerson{stringname=1;int32id=2;// Unique ID number for this person.stringemail=3;enumPhoneType{PHONE_TYPE_UNSPECIFIED=0;PHONE_TYPE_MOBILE=1;PHONE_TYPE_HOME=2;PHONE_TYPE_WORK=3;}messagePhoneNumber{stringnumber=1;PhoneTypetype=2;}repeatedPhoneNumberphones=4;google.protobuf.Timestamplast_...
生成go文件 protoc --go_out=. *.proto 生成后的文件: // Code generated by protoc-gen-go. DO NOT EDIT.// source: test.proto// 包名,通过protoc生成go文件时packagetestprotobufimport(fmt"fmt"proto"github.com/golang/protobuf/proto"math"math")// Reference imports to suppress errors if they a...
packagemainimport("fmt""google.golang.org/protobuf/proto""io/ioutil""log""proto/person")//因为go run会有临时目录,所以使用相对路径没有那么方便,这里就使用绝对路径了,因为使用的是readfile,所以需要自己先创建这个文件。当然,修改代码,使用其他文件的API也可以的。constfileName ="C:\\Users\\yyjeqhc\...
将字节数据转为Protobuf所生成的对象; 数据流协议为7E头7E尾,简单起见这里就不使用转码了,字节格式如下。 7E-类型-经度-纬度-状态长度-状态 str :="7E0106C1FC5A0160C9640231327E"buf := bytes.NewBuffer(toBytes(str)) p :=&test.Position{}//跳过头buf.Next(1)//读取类型varbbyteb...
Protobuf核⼼的⼯具集是C++语⾔开发的,在官⽅的protoc编译器中并不⽀持Go语⾔。要想基于 .proto⽂件⽣成相应的Go代码,需要安装相应的插件。 cmd输入: 代码语言:javascript 复制 go install google.golang.org/protobuf/cmd/protoc-gen-go@latest ...
gogoprotobuf有两个插件可以使用 protoc-gen-gogo:和protoc-gen-go生成的文件差不多,性能也几乎一样(稍微快一点点) protoc-gen-gofast:生成的文件更复杂,性能也更高(快5-7倍) 在官网上可以看到: Getting Started There are several ways to use gogoprotobuf, but for...
一. protobuf包的安装 1.下载protoc编译器下载链接: protocolbuffers/protobuf[https://github.com/protocol...
protoc-gen-go 是 protobuf 编译插件系列中的Go版本,protoc-gen-to 使用Golang编写。 在Golang中使用protobuf需提前安装 protoc-gen-to工具,用于将.proto文件转换为Golang代码。 代码语言:javascript 复制 go get -u github.com/golang/protobuf/protoc-gen-go protoc-gen-go将自动安装到$GOPATH/bin目录下 pr...
将字节数据转为Protobuf所生成的对象; 数据流协议为7E头7E尾,简单起见这里就不使用转码了,字节格式如下。 7E-类型-经度-纬度-状态长度-状态 str := "7E0106C1FC5A0160C9640231327E" buf := bytes.NewBuffer(toBytes(str)) p :=&test.Position{} ...