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
import (
"io/ioutil"
"os"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/generator"
)
[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;
}
}
测试代码如下:
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