Details of how to install and use protobuf in go

  • 2020-06-07 04:40:29
  • OfStack

Introduction to the

This article mainly introduces the go language installation and use of protobuf related content, share for your reference and learning, the following words do not say much, let's have a look at the detailed introduction.

protobuf is a language-independent and platform-independent data serialization tool developed by Google, which can be used in many scenarios such as rpc or tcp communication. In layman's terms, if the client and server are using different languages, a data structure is defined on the server, converted to a byte stream through protobuf, and then transmitted to the client for decoding to obtain the corresponding data structure. That's the magic of protobuf. Moreover, its communication efficiency is extremely high, "the serialization size of a message data with protobuf is 1/10 of that of json, 1/20 of that of xml, and 1/10 of that of base 2 serialization".

The installation

Compile the compiler protoc for installing protobuf


  wget https://github.com/google/protobuf/releases/download/v2.6.1/protobuf-2.6.1.tar.gz
  tar zxvf protobuf-2.6.1.tar.gz
  cd protobuf-2.6.1./configure
  make
  make install

perform protoc  -h Check to see if the installation was successful

Install the plug-in ES31en-ES32en-ES33en, which is an go program, and compile it to write the executable execution path to the environment variable


go get github.com/golang/protobuf/protoc-gen-go

Get proto package


go get github.com/golang/protobuf/proto

Used in go

protobuf is used by writing data structures to the.proto file. Compiling with the protoc compiler (indirectly using plug-ins) yields a new go package containing the data structures available in go and 1 helper methods.

Prepare test.proto file


 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;
  }
 }

Compile:

perform protoc --go_out=. *.proto Generate the test.pb.go file

Place the test.pb.go file into the example folder (corresponding to package above) as the example package

try


 package main

 import (
  "log"

  "github.com/golang/protobuf/proto"
  "example"
 )

 func main() {
  test := &example.Test {
   Label: proto.String("hello"),
   Type: proto.Int32(17),
   Reps: []int64{1, 2, 3},
   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)
  }
  // Now test and newTest contain the same data.
  if test.GetLabel() != newTest.GetLabel() {
   log.Fatalf("data mismatch %q != %q", test.GetLabel(), newTest.GetLabel())
  }
  //test.GetOptionalgroup().GetRequiredField()
  //etc
 }

1 some correspondence

message Test pair is struct structure, and its attribute field has corresponding get method, which can be used in go test.GetLabel() , test.GetType() Gets the properties of the test object OptionalGroup corresponds to struct embedded in struct The repeated attribute in proto file is for the slice structure test.Reset() You can set all of its properties to a value of 0 Marshal and Unmarshal make it easy to encode and decode

These just 1 characteristics, want careful study can view github wiki: https: / / github com/golang/protobuf

conclusion


Related articles: