GOLANG-PROTOBUF Quick Start Guide

Source: Internet
Author: User
Tags automake

What is Protobuf

Google Protocol Buffer (protobuf) is a mixed-language data standard within Google Inc.

Protocol buffers is a lightweight and efficient structured data storage format that can be used for structured data serialization, or serializing. It is ideal for data storage or RPC data interchange formats. It can be used in the communication protocol, data storage and other fields of the language-independent, platform-independent, extensible serialization structure data format.

How to install Protobuf

    • Get protobuf source on GitHub, the Windows system can download EXE files directly: https://github.com/google/protobuf/releases

    • Steps for installing MacOS and Linux environments using the source code

# 获取源码包wget https://github.com/google/protobuf/archive/v3.5.0.tar.gz# 解压缩并进入源码目录tar -zxvf v3.5.0.tar.gzcd protobuf-3.5.0# 生成configure文件./autogen.sh# 编译安装./configuremakemake checkmake install

In the execution ./autogen.sh process may be due to lack of automake dependent library error:autoreconf:failed to run aclocal:no such file or directory, in order to resolve this error, In Linux systems can be passed sudo yum install automake or sudo apt-get install automake installed Automake, the MacOS system can be brew install automake installed by Automake.

    • Detect if Protobuf has been successfully installed
      Command line execution protoc --version . The Windows system needs to add the directory where the Protoc.exe file resides to the environment variable.

How to use Protobuf

PROTOBUF supports cross-language use, many mainstream development languages have protobuf support libraries, and we use Golang to demonstrate how to use PROTOBUF for data serialization.

    • Installing PROTOBUF-related libraries in Golang
go get -u github.com/golang/protobuf/{protoc-gen-go,proto}
    • Writing a. proto file
      $GOPATH/src/test/protobuf/pb/user.proto
    package pb;    message user {     int32 id = 1;     string name = 2;    }    message multi_user {     repeated user users = 1;    }
    • Generate data manipulation codes based on. Proto files
      protoc --go_out=. user.proto
      Executes the command completion, user.proto generating a file in the same directory as the file sibling user.pb.go

    • Data Serialization Demo
      $GOPATH/src/test/protobuf/main.go

   package main    import (     "log"     "test/protobuf/pb"     "github.com/golang/protobuf/proto"    )    func main() {     user1 := pb.User{     Id:   *proto.Int32(1),     Name: *proto.String("Mike"),     }     user2 := pb.User{     Id:   2,     Name: "John",     }     users := pb.MultiUser{     Users: []*pb.User{&user1, &user2},     }     // 序列化数据     data, err := proto.Marshal(&users)     if err != nil {     log.Fatalln("Marshal data error: ", err)     }     println(users.Users[0].GetName()) // output: Mike     // 对已序列化的数据进行反序列化     var target pb.MultiUser     err = proto.Unmarshal(data, &target)     if err != nil {     log.Fatalln("Unmarshal data error: ", err)     }     println(target.GetUsers()[1].Name) // output: John    }

Related extensions

Sublime Plug-in: Protobuf Syntax hightlighting

Resources

    • Protocol Buffers Concise tutorial
    • Linux install Google protobuf (verbose)
    • Golang serialization Protobuf
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.