Mac使用grpc时候,安装protobuf
发布日期:2021-05-10 07:17:33 浏览次数:21 分类:精选文章

本文共 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
    1. ������������������������������������������������������������������������������������

    2. ���������������

      ������������������������������������������������������������������������������������������������������������������ protobuf ���Go������������������������������������������������������

    上一篇:Linux下 su命令与su - 命令有什么区别?
    下一篇:go new和make的区别

    发表评论

    最新留言

    留言是一种美德,欢迎回访!
    [***.207.175.100]2025年04月22日 11时27分31秒