golang与protobuf
编写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=. *.proto
编译
golang的测试代码:
package main
import (
"log"
// 辅助库
"github.com/golang/protobuf/proto"
// test.pb.go 的路径
"foodemo"
"fmt"
)
func main() {
fmt.Println("hello")
// 创建一个消息 Test
test := &example.Test{
// 使用辅助函数设置域的值
Label: proto.String("hello"),
Type: proto.Int32(17),
Optionalgroup: &example.Test_OptionalGroup{
RequiredField: proto.String("good bye"),
},
}
// 进行编码
data, err := proto.Marshal(test)
if err != nil {
log.Fatal("marshaling error: ", err)
}
// 进行解码
newTest := &example.Test{}
err = proto.Unmarshal(data, newTest)
if err != nil {
log.Fatal("unmarshaling error: ", err)
}
// 测试结果
if test.GetLabel() != newTest.GetLabel() {
log.Fatalf("data mismatch %q != %q", test.GetLabel(), newTest.GetLabel())
}else{
fmt.Println("the same")
}
}