
本文共 4648 字,大约阅读时间需要 15 分钟。
������Go���������������������ProtoBuf������
���������������������������������������������������������������������������������������������Go��������������������������������� v1.14.12 darwin/amd64
������������������������������������������Go���������������������������������������������������
# ���������������Go������curl -o- https://golang.org/dl/go1.14.12-darwin-amd64.dmg
������������������������������������������Go���������������������������������
������_protobuf���������������
������������������������������������������protoc-gen-go
������������������������������������������ protobuf������������(.proto)���������Go������������������������������������������������������
go get github.com/golang/protobuf/protoc-gen-gocd
������������������������������������������������������������������������������������������������$HOME/.bashrc
���$HOME/.zshrc
���������������������������
source ~/.bashrc
������������������������Protobuf������������������
���������������
���������������������������������������Protobuf���������������Go������������������������������������������������������������������������������������
go get github.com/golang/protobuf/protocd
������������������������������������������������������������������
buildgo install
���������������proto������
������������������������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; }}
������������������������������������Go������������
protoc --go_out=. test.proto
���������������������������test.pb.go
������������Protobuf���������������������Go������������������������
������Protobuf���������Go������������������
������������������������test.pb.go
���������������������Protobuf���������������������������Go������������������������������������������������������
������������Go������������������Protobuf���������������������������Go������������������������������������������������������������������camelCase������
������setter������������������������������setter������������������������������������������������������������������������proto.String
���proto.Int32
���������������������������
������getter������������������������������������������������������getter������������������������������������������������������getter���������������������������
���������������������������������������������������������������������������������������������������������nil
���
���������������������������������������������������������������������������������������������
������������������������������������������������������EnumName_EnumValue
���������������FOO_X
���
���������������������������proto.Marshal
���������������������������proto.Unmarshal
���������������������
���������Protobuf���������Go������
������������������������������������������������Go������������Protobuf������������������������������������������
package mainimport ( "log" "github.com/golang/protobuf/proto" "example")func main() { // ������������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()) } // ������������������������������������������������������ fmt.Printf("Enum value: %s\n", test.GetFOO())}
������������������������������
- ���������������Protobuf������������������������������
- ������
proto.Marshal
��������������������������������������� - ������
proto.Unmarshal
��������������������������������������������������� - ���������������������������������
- ���������������������������
������������������
������������������������������������������������������������������������������������������
$GOPATH/bin
������������������PATH
���������������������������export PATH=$PATH:$GOPATH/bin
- ������������������������������������������������������������������������������������
���������������
������������������������������������������������������������������������������������������������������������������ protobuf ���Go������������������������������������������������������
发表评论
最新留言
关于作者
