您的位置:首页 > 编程语言 > Go语言

如何在Go中使用Protobuf

2018-12-03 17:35 801 查看

如何在Go中使用Protobuf

nellson · 2016-06-27 21:00:06 · 4720 次点击 · 预计阅读时间 3 分钟 · 大约2小时之前 开始浏览    

这是一个创建于 2016-06-27 21:00:06 的文章,其中的信息可能已经有所发展或是发生改变。

Protobuf对于Golang通过插件进行支持,因些需要安装protoc的执行环境,下面我们来一步步看下,如何搭建一个编译环境。

 

1. 安装protoc

 

2. 下载并安装protobuf-go插件

从github上下载插件,并解压(https://github.com/golang/protobuf),得到以下的目录

 

drwxr-xr-x 6 root root 4096 Jun 16 15:45 .
drwxr-xr-x 3 root root 4096 Jun 16 15:48 ..
-rw-r--r-- 1 root root  173 Jun 15 06:31 AUTHORS
-rw-r--r-- 1 root root  170 Jun 15 06:31 CONTRIBUTORS
drwxr-xr-x 3 root root 4096 Jun 15 06:31 jsonpb
-rw-r--r-- 1 root root 1583 Jun 15 06:31 LICENSE
-rw-r--r-- 1 root root 2080 Jun 15 06:31 Makefile
-rw-r--r-- 1 root root 1955 Jun 15 06:31 Make.protobuf
drwxr-xr-x 4 root root 4096 Jun 15 06:31 proto
drwxr-xr-x 7 root root 4096 Jun 16 15:42 protoc-gen-go
drwxr-xr-x 8 root root 4096 Jun 15 06:31 ptypes
-rw-r--r-- 1 root root 7149 Jun 15 06:31 README.md


这时,执行make install,多半是不会成功的,一般会报找不到对应的文件,原因在于go源文件中指定的目录位置是这样的

 

 

import (
"io/ioutil"
"os"

"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/generator"
)


因此,要求我们把当面下载的文件放到$GOROOT对应的目录下,并且把目录名改成指定的名称,比如我的GOROOT=/usr/local/go,那我就把解压后的目录改名为protobuf,并在/usr/local/go下创建/usr/local/go/src/github.com/golang/目录,把protobuf目录整体mv过去,再执行make install,执行结果如下:

 

 

[root@SH-todo-1412181717 /usr/local/go/src/github.com/golang/protobuf]# make install
go install ./proto ./jsonpb ./ptypes
go install ./protoc-gen-go


说明执行成功了。

 

3. 用法举例

下面我们来说明如何把*.proto文件生成*.go文件,同时在程序中序列及反序列化

a) 创建一个test.proto文件

 

package example;

enum FOO { X = 17; };

message Test {
required string label = 1;
optional int32 type = 2 [default=77];
repeated int64 reps = 3;
optional group OptionalGroup = 4 {
required string RequiredField = 5;
}
}


执行protoc --go_out=. test.proto,得到test.pb.go

 

测试代码如下:

 

package main

import (
"log"
"fmt"
// 辅助库
"github.com/golang/protobuf/proto"

// test.pb.go 的路径
"example"
)

func main() {
// 创建一个消息 Test
test := &example.Test{
// 使用辅助函数设置域的值
Label: proto.String("hello"),
Type:  proto.Int32(17),
Optionalgroup: &example.Test_OptionalGroup{
RequiredField: proto.String("good bye"),
},
}

fmt.Printf("Label:%s Type:%d\n", test.GetLabel(), test.GetType())

*(test.Label) = "hello go"
*(test.Type) = 18

// 进行编码
data, err := proto.Marshal(test)
if err != nil {
log.Fatal("marshaling error: ", err)
}

fmt.Printf("Binary Len:%d\n", len(data))

// 进行解码
newTest := &example.Test{}
err = proto.Unmarshal(data, newTest)
if err != nil {
log.Fatal("unmarshaling error: ", err)
}

fmt.Printf("Label:%s Type:%d\n", test.GetLabel(), test.GetType())

// 测试结果
if test.GetLabel() != newTest.GetLabel() {
log.Fatalf("data mismatch %q != %q", test.GetLabel(), newTest.GetLabel())
}
}


执行结果如下:

 

 

Label:hello Type:17
Binary Len:24
Label:hello go Type:18
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: