instructions

The Demo source code

The Protocol Buffer is a highly efficient serialization deserialization tool from Google that allows you to customize a data structure and then read and write it using code generated by a code generator in the corresponding language. While you’ll still use JSON with the front end, you can try using the Protocol Buffer to improve performance elsewhere.

Here’s a summary of how to use the Protocol Buffer in GoLang.

Go Environment Configuration

Download the protobuf

git clone https://github.com/protocolbuffers/protobuf.git
Copy the code

Install Ubuntu Linux ()

(1) Install dependency tools

sudo apt-get install autoconf automake libtool curl make g++ unzip libffi-dev -y
Copy the code

(2) Access the Protobuf file

cd protobuf/
Copy the code

(3) Perform installation detection and generate automatic installation scripts

./autogen.sh
./configure
Copy the code

(4) C code compilation and installation

make
sudo make install
Copy the code

(5) Refresh the Linux shared library relationship

sudo ldconfig
Copy the code

(6) Test the Protobuf compilation tool

protoc -h
Copy the code

If no error message is displayed, the installation is successful.

Get GoLang’s Proto package

(1) to download

go get -v -u github.com/golang/protobuf/proto
Copy the code

(2) Enter the folder to compile

cd $GOPATH/src/github.com/golang/protobuf/protoc-gen-go/
go build
Copy the code

(3) Copy executable files

Place the generated protoc-gen-go executable file in the /bin directory.

sudo cp protoc-gen-go /bin/
Copy the code

Protoc -gen-go if the completion is successful, the tool succeeds if no error is reported.

Go use protobuf

Create a.proto file

The basic format is as follows:

syntax = "proto3"; Protobuf protocol version number package pb; Protobuf message Person {string name = 1; Int32 age = 2; repeated string hobby = 3; []string}Copy the code

Generate Go data structures

In the. Proto directory, run the following command:

protoc --go_out=.  *.proto
Copy the code

The corresponding. Go file will be generated in the current directory, where the data structure of go can be found.

type Person struct {
	Name                 string   `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
	Age                  int32    `protobuf:"varint,2,opt,name=age,proto3" json:"age,omitempty"`
	Hobby                []string `protobuf:"bytes,3,rep,name=hobby,proto3" json:"hobby,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}
Copy the code

Note that this file can only be generated and cannot be manually modified.

Use the protobuf

Create a Person object in the main function and serialize and deserialize it,

package main

import (
	"protobufDemo/pb"
	"github.com/golang/protobuf/proto"
	"fmt"
)

func main(a) {

	/ / the serialization
	person := &pb.Person{
		Name:"Jack",
		Age:18,
		Hobby:[]string{"sing"."dance"."basketball"."rap"},
	}

	binaryData, err := proto.Marshal(person)
	iferr ! =nil {
		fmt.Println("proto.Marshal err:",err)
	}

	// deserialize
	newPerson := &pb.Person{}
	err = proto.Unmarshal(binaryData,newPerson)
	iferr ! =nil {
		fmt.Println("proto.Unmarshal err:",err)
	}

	fmt.Println("Raw data before serialization :",person)
	fmt.Println("Deserialize the data :",newPerson)
}

Copy the code

After execution, you can see that Both Person and newPerson enjoy singing, dancing, basketball, and rap.

Name :"Jack" age:18 Hobby :"sing" Hobby :"dance" hobby:" Basketball "hobby:"rap" Deserialization name:"Jack" age:18 hobby:"sing" hobby:"dance" hobby:"basketball" hobby:"rap"Copy the code