How to use Protobuf in Go

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Protobuf for Golang through plug-in support, because some need to install PROTOC execution Environment, below we take a step-by-step look at, how to build a compilation environment.


1. Installing PROTOC


2. Download and install the Protobuf-go plugin

Download the plugin from GitHub and unzip (HTTPS://GITHUB.COM/GOLANG/PROTOBUF) to get the following directory

Drwxr-xr-x 6 root root 4096 June 15:45. drwxr-xr-x 3 root root 4096 June 16 15:48. -rw-r--r--1 root root  173 June 06:31 authors-rw-r--r--1 root root  June 06:31 contributorsdrwxr-xr-x 3 ro OT root 4096 June 06:31 jsonpb-rw-r--r--1 root root 1583 June 06:31 license-rw-r--r--1 root root 2080 June 06:31 M akefile-rw-r--r--1 root Root 1955 June 06:31 make.protobufdrwxr-xr-x 4 root root 4096 June 06:31 protodrwxr-xr-x 7 ro OT root 4096 June 15:42 protoc-gen-godrwxr-xr-x 8 root root 4096 June 06:31 ptypes-rw-r--r--1 root root 7149 June 15 0 6:31 readme.md

At this point, the implementation of make install, mostly will not be successful, generally will not find the corresponding file, because the go source file specified in the directory location is this

Import (    "Io/ioutil"    "OS" "    Github.com/golang/protobuf/proto"    "github.com/golang/protobuf/ Protoc-gen-go/generator ")

Therefore, asked us to put the files in the face of the download to the $goroot corresponding directory, and the directory name to the specified name, such as my goroot=/usr/local/go, then I will be the extracted directory renamed to Protobuf, and under/usr/local/go to create /usr/local/go/src/github.com/golang/directory, the Protobuf directory of the whole MV past, then execute make install, the results are as follows:

[root@sh-todo-1412181717/usr/local/go/src/github.com/golang/protobuf]# make Installgo install./proto./JSONPB./ Ptypesgo install./protoc-gen-go

The instructions were successfully executed.

3. Examples of usage

Let's show how to generate a *.go file for the *.proto file, while sequencing and deserializing the program

A) Create a 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 Option Algroup = 4 {Required String requiredfield = 5;}}

Perform Protoc--go_out=. Test.proto, get test.pb.go.

The test code is as follows:

Package Mainimport ("Log" "FMT"//Auxiliary Library "Github.com/golang/protobuf/proto"//test.pb.go Path "example") F UNC main () {//Creates a message in test test: = &example. test{//Use the auxiliary function to set the value of the field Label:proto. String ("Hello"), Type:proto. Int32 (+), Optionalgroup: &example. test_optionalgroup{Requiredfield:proto. String ("Good Bye"),},} FMT. Printf ("label:%s type:%d\n", test. Getlabel (), test. GetType ()) * (Test. Label) = "Hello Go" * (Test. Type) = 18//Encode data, err: = Proto. Marshal (test) if err! = Nil {log. Fatal ("Marshaling Error:", err)} FMT. Printf ("Binary len:%d\n", Len (data))//decode Newtest: = &example. test{} err = Proto. Unmarshal (data, newtest) if err! = Nil {log. Fatal ("Unmarshaling error:", err)} FMT. Printf ("label:%s type:%d\n", test. Getlabel (), test. GetType ())//testing results if test. Getlabel ()! = Newtest.getlabel () {log. Fatalf ("Data mismatch%q! =%q", tEst. Getlabel (), Newtest.getlabel ())}}

The results of the implementation are as follows:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.