PROTOBUF installation and simple test cases based on the Go language

Source: Internet
Author: User

  • Go to the official website first download PROTOBUF source code

    Https://github.com/google/protobuf/releases

    You can download the local and upload to the virtual machine first

    I chose source code (tar.gz).

  • Install a dependent package (error may occur if a package is missing)

    Yum install-y gcc gcc-c++ autoconf automake Libtool Curl Make g++ Unzip


  • After decompression, enter the protobuf-3.5.1 directory and run

    ./autogen.sh command

  • Execute the./configure command

  • Make && make install

  • Detect PROTOBUF is installed successfully?

    Protoc-h

  • Because you want to use the go language, you need to install the appropriate plugin

      1. First to the official website to download the plugin source code to Local:

    https://github.com/golang/protobuf/releases

    Uploading to a virtual machine for decompression

    TAR-ZXVF protobuf-1.1.0.tar.gz

      1. Renaming

      2. MV Protobuf-1.1.0.tar.gz Protobuf

      1. What is the value of querying goroot, as mine is:

        Echo $GOROOT

        /usr/local/gohome/go

      2. Under the Goroot directory, create the following directory Src/github.com/golang

        Mkdir-p /usr/local/gohome/go/src/github.com/golang

      3. Move the Protobuf directory to the $goroot/src/github.com/golang directory

      4. Enter $GOROOT/SRC/GITHUB.COM/GOLANG/PROTOBUF

      5. Make install

    Example:

    Create a file manually first Test.proto

    Specify version//note Proto3 and Proto2 are somewhat different syntax =  "Proto3";  //package name, Protoc phone type when the go file is generated by package test;// /enum Type the first field must be a 0enum phonetype {    home = 0;      work = 1; }  //Mobile  message Phone {      Phonetype type = 1;         string number  = 2; }  //man  message person {     // The number that follows indicates the identification number      int32 id = 1;      string  name = 2;     //repeated means repeatable      //can have multiple phones      repeated phone phones = 3; }  //Contact Book   message contactbook {     repeated person persons = 1;  }

    Execute the following command to generate the appropriate code file

    Protoc--go_out=. *.proto

    Or

    Protoc--go_out=. Test.proto

    In the go language, how to use it?

    1. First create the test package, and then upload the generated test.pb.go to the test package

    2. Writing test Cases

    The contents are as follows:

    Package Mainimport ("FMT" "Github.com/golang/protobuf/proto" "Io/ioutil" "OS" "xingej-go/xingej-go/xingej-go666/ Protobuf/test ") Func write () {p1: = &test. person{id:1, Name: "Spark",} book: = &test. contactbook{} book. Persons = Append (book. Persons, p1)//encoded data, _: = Proto. Marshal (book)//writes data to file Ioutil. WriteFile ("./pb_test.txt", data, OS.) Modeperm)}func Read () {data, _: = Ioutil. ReadFile ("./pb_test.txt") Book: = &test. contactbook{}//Decode data proto. Unmarshal (data, book) for _, V: = Range book. Persons {fmt. Println (V.id, V.name)}}func main () {write () read ()}

    This article tests the sample and references the following article:

    Https://www.cnblogs.com/jkko123/p/7161843.html


    PROTOBUF installation and simple test cases based on the Go language

    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.