Golang using Protobuf

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

Installing the PROTOBUF Compilation tool

Download the latest version Https://github.com/golang/protobuf Official website Https://developers.google.com/protocol-buffers/docs/proto3 after decompression execution
./autogen.sh    If you encounter a program that is not supported, install it./configuremakemake Installation

installing the Golang Support library

Download Https://github.com/golang/protobuf Building the catalog in the project SRC directory
    github.com/golang/protobuf/
Copy all downloaded protobuf to this directory CD to this directory to execute
    Make
The Protoc-gen-go executable is compiled, which is provided to the PROTOBUF compilation tool using the

Test

Create a folder in the code directory where proto files are placed, such as protocfg create. proto file configuration PROTOBUF data structures such as
Syntax = "Proto3";p ackage prototest;enum FOO {X = 0;}; Message Test {string label = 1;int32 type = 2; int64 reps = 19;}
Execute in this directory
/usr/local/bin/protoc--plugin={}bin/protoc-gen-go--go_out=. Test.proto
will be generated test.bp.go This file is used in the GO program:
Package Mainimport ("Demo/prototest" "FMT" "Libs/plog" "Github.com/golang/protobuf/proto") func Test () {fmt. PRINTLN ("Begin Testlog ...")//proto. Bool (FALSE) Test: = &prototest. Test{label: "Hello", type:17, Reps:34}data, err: = Proto. Marshal (test) if err! = Nil {plog. Println (Plog. Warning, "Demo", Err. Error ())} else {untest: = &prototest. Test{}err: = Proto. Unmarshal (data, untest) if Err = = Nil {fmt. Println ("Undata", Untest.label, Untest.reps, Untest.type)}}fmt. PRINTLN ("Data", data)}func main () {Test ()}








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.