Tutorial using protocol buffer in Golang language

Source: Internet
Author: User

Code Warehouse Address

First, Introduction

Protobuf is a platform-independent, language-independent, extensible serialized structured data format from Google. So it is suitable for the data exchange format which is used for data storage and communication between different languages.As long as the same protocol format is implemented, the same proto files are compiled into different language versions and added to their respective projects, so that different languages can parse data that is serialized by Protobuf in other languages. At present, the official website provides the support of C++,python,java,go and other languages.

The following advantages are with respect to JSON and XML:

    • Simple

    • Small size: Message size requires only 1/10 ~ 1⁄3 of XML

    • Faster: 20 ~ 100 times faster parsing speed than XML

    • Using the Protobuf compiler, you can generate data access code that is easier to use in programming

    • For better compatibility, one of the principles of protobuf design is to be able to support downward or upward compatibility.

Note: This article mainly focuses on the use of protobuf, so there is no download, installation of the operating procedures.

Second, create a Test.proto file and generate the Test.pb.go file

Syntax = "Proto3";  statement using the PROTO3 protocol package test  ; package name, generate Go file via Protoc enum phonetype{    = 0;     = 1;} Message phone{  //     keyword = 1;     = 2;} Message person{    = 1;     = 2;     = 3;  The field can be repeated any number of times (including 0 times    )}message contactbook{= 1;}

Run the following command to generate the Test.pb.go file

> Protoc--go_out=. *.proto

Note The package name is identical to the folder name.

Third, the use of protobuf in the Go language

Package Mainimport ("FMT"    "Io/ioutil"    "OS"    "PFTEST/PF"    "Pftest/github.com/golang/protobuf/proto") Func write () {p1:= &PF. person{Id:1, Name:"Xiao Zhang", phones: []*PF. phone{{PF. Phonetype_home,"11111111"}, {pf. Phonetype_work,"22222222"},} ,} p2:= &PF. person{Id:2, Name:"Xiao Wang", phones: []*PF. phone{{PF. Phonetype_home,"33333333"}, {pf. Phonetype_work,"44444444"},} ,} p3:= &PF. person{Id:3, Name:"Xiao Li", phones: []*PF. phone{{PF. Phonetype_home,"55555555"}, {pf. Phonetype_work,"66666666"},} ,} book:= &PF. contactbook{} book. Persons=Append (book. Persons, p1) book. Persons=Append (book. Persons, p2) book. Persons=Append (book. Persons, p3) data, _:=Proto. Marshal (book) Ioutil. WriteFile ("./test.txt", data, OS. Modeperm)}func Read () {date, _:= Ioutil. ReadFile ("./test.txt") Book:= &PF. contactbook{} proto. Unmarshal (date, book) for_, V: =Range Book. Persons {fmt. Println (V.id, V.name) for_, VV: =range V.phones {fmt. PRINTLN (vv. Type, VV. Number)}}}func main () {write () read ()}

The results of the operation are as follows:

Iv. references

Google Proto2 Guidegoogle Proto3 Guide

Golang Tutorials for using protobuf

PROTOBUF Introduction and use

Go Protobuf3 Grammar Guide _ Bird's Nest

Installation and use of Golang version Protobuf

V. Summary

If you are interested in understanding the operation of the data, the whole process of compression, you can consider using PROTOBUF.

It is like a code converter, you only need to write the underlying protocol, and then use the Protobuf ready-made tools to generate the corresponding language source files, in order to achieve the project using the same protocol format for data transmission purposes.

This is a new skill I learned at work!

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.