Protobuf-3.6.1 Installation and Golang use

Source: Internet
Author: User

Write in front

PROTOBUF is a data description language developed by Google, capable of serializing structured data for data storage, communication protocols, and official versions that support Go, C + +, Java, Python, and Community editions to support more languages.
The following advantages are with respect to JSON and XML:
Small Size: Message size requires only 1/10 ~ 1⁄3 of XML
faster: 20 ~ 100 times faster parsing speed than XML
High Integration : Using the Protobuf compiler, you can generate data access code that is easier to use in programming
better compatibility : One of the principles of protobuf design is to be able to be well-compatible with down or up

Installation

1. Get the Protobuf compiler from Https://github.com/google/protobuf/releases Protoc

wget https://github.com/google/protobuf/releases/download/v3.6.1/protobuf-all-3.6.1.tar.gz tar zxvf protobuf-all-3.6.1.tar.gzcd protobuf-3.6.1./configuremakemake installprotoc   -hprotoc --version

Problems encountered and their solutions
[1] libprotoc.so.17:cannot open Shared object file:no such file or directory

bjlvxin:~/下载/protobuf-3.6.1$ protoc --versionprotoc: error while loading shared libraries: libprotoc.so.17: cannot open shared object file: No such file or directory解决方案:执行:export LD_LIBRARY_PATH=/usr/local/libbjlvxin:~/下载/protobuf-3.6.1$ export LD_LIBRARY_PATH=/usr/local/lib/  bjlvxin:~/下载/protobuf-3.6.1$ protoc   --versionlibprotoc 3.6.1

2. Obtain the PROTOBUF plug-in protoc-gen-go provided by GOPROTOBUF (placed under $GOPATH/bin, $GOPATH/bin should be added to the PATH environment variable so PROTOC can find protoc-gen-go)
This plugin is used by PROTOC to compile the. proto file as a Golang source file, which allows you to use the message defined in the. proto file.

go get github.com/golang/protobuf/protoc-gen-gocd $GOPATH/src/github.com/golang/protobuf/protoc-gen-gogo buildgo installvi ~/.bashrc 将$GOPATH/bin 加入环境变量:export PATH=$PATH:$GOPATH/binsource ~/.bashrc

3. Get the support library provided by GOPROTOBUF, including functions such as encoding (marshaling), decoding (unmarshaling), etc.

go get github.com/golang/protobuf/protocd $GOPATH/src/github.com/golang/protobuf/protogo buildgo install

Use

This article uses Golang to PROTOBUF.
1. Create a new Golang project through Goland:



2. Write the Person.proto in the example package

syntax = "proto3";package example;//  person 会生成 Person 命名的结构体message person {                    int32 id = 1;                    string name = 2;}//  all_person 会按照驼峰规则自动生成名为AllPerson 的结构体message all_person {                        repeated person Per = 1;}

3. Enter the proto directory of the Protobuf-golang project and use PROTOC to compile the Person.proto

protoc --go_out=. person.proto

After execution, the corresponding go file will be generated in the Proto directory: Person.pb.go
4. Main.go documents for the preparation of the project:

/*Copyright 2018 JD-Tigercreated by lvxin  at 18-8-13 下午12:03*/package mainimport (    example "github.com/lvxin1986/protobuf-golang/proto"    "log"    "github.com/golang/protobuf/proto"    "fmt")func main() {    // 为 AllPerson 填充数据    //使用protobuf的封装类型定义    p1 := example.Person{        Id:*proto.Int32(1),        Name:*proto.String("lvxin"),    }    //使用golang的原始类型定义    p2 := example.Person{        Id:2,        Name:"gopher",    }    all_p := example.AllPerson{        Per:[]*example.Person{&p1, &p2},    }    // 对数据进行序列化    data, err := proto.Marshal(&all_p)    if err != nil {        log.Fatalln("Mashal data error:", err)    }    // 对已经序列化的数据进行反序列化    var target example.AllPerson    err = proto.Unmarshal(data, &target)    if err != nil{        log.Fatalln("UnMashal data error:", err)    }    for k,v := range target.Per {        fmt.Println("person[",k,"]:",v.Name)    }}

5. Run directly after development:



6. The results of the operation are as follows:

GOROOT=/software/servers/go1.10.3 #gosetupGOPATH=/sourcecode/go/work #gosetup/software/servers/go1.10.3/bin/go build -i -o /tmp/___go_build_main_go /sourcecode/go/work/src/github.com/lvxin1986/protobuf-golang/main.go #gosetup/tmp/___go_build_main_go #gosetupperson[ 0 ]: lvxinperson[ 1 ]: gopherProcess finished with exit code 0

Finish the call.

Reference documents

Https://www.jianshu.com/p/1a3f1c3031b5
https://segmentfault.com/a/1190000010477733
Http://lihaoquan.me/2017/6/29/how-to-use-protobuf.html
Https://www.pythonxyz.com/10038-install-protobuf-in-ubuntu.xyz

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.