Ubuntu down go language using protobuf

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

1. Compile and install Protobuf (protobuf no release on Go)

   为了从源码安装protobuf,先要安装一些工具:包括autoconf、automake、libtool、curl(用于下载gmock)、make、 g++、 unzip。      在ubuntu可以使用如下命令安装这些依赖:
   sudo apt-get install autoconf automake libtool curl make g++ unzip
然后使用 git clone  https://github.com/google/protobuf命令下载protobuf的源码包。接者使用命令 cd protobuf进入源码库中然后使用  ./autogen.sh运行脚本生成需要的配置脚本接着执行如下的一系列命令来编译安装protocol buffer的complier./configuremakemake checksudo make installsudo ldconfig   最后的一个命令是用于刷新共享库的缓存的。,最后一个命令我的ubuntu无法执行

2. Installing Proto-gen-go

 命令如下: go get -u github.com/golang/protobuf/proto           go get -u github.com/golang/protobuf/protoc-gen-go然后将环境变量GOPATH定义的目录下的bin目录加入到环境变量PATH中。命令如下:vi  ~/.bashrc然后在该文件最后加上:export PATH="$PATH:$GOPATH/bin"即可。然后调用 source ~/.bashrc

3. Define a proto file Test.proto as follows:

package example;enum FOO { X = 17; };message Test {required string label = 1;optional int32 type = 2 [default=77];repeated int64 reps = 3;optional group OptionalGroup = 4 {required string RequiredField = 5;}}

Execute the command in the folder that corresponds to the file: Protoc--go_out=. The *.proto is used to generate the. pb.go file. If this error occurs during the execution of this command, Protoc-gen-go:program not found or was not executable, indicating that the protoc-gen-go was not added to the PATH environment variable, The directory where the file is located should be added to the path variable. This file is stored in the bin subdirectory of the environment variable Gopath directory.

4. Write the test code:

 首先要将上一节生成的example包放入GOPATH目录下的src文件夹下: 然后建立如下的测试代码:
package mainimport (    "fmt"    "github.com/golang/protobuf/proto"    // test.pb.go 的路径    "example")func main() {    // 创建一个消息 Test    test := &example.Test{        // 使用辅助函数设置域的值        Label: proto.String("hello"),        Type:  proto.Int32(17),        Optionalgroup: &example.Test_OptionalGroup{            RequiredField: proto.String("good bye"),        },    }    // 进行编码    data, err := proto.Marshal(test)    if err != nil {        fmt.Println("marshaling error: ", err)    }    // 进行解码    newTest := &example.Test{}    err = proto.Unmarshal(data, newTest)    if err != nil {        fmt.Println("unmarshaling error: ", err)    }    // 测试结果    if test.GetLabel() != newTest.GetLabel() {        fmt.Println("data mismatch %q != %q", test.GetLabel(), newTest.GetLabel())    }    fmt.Printf("%+v", newTest)}

Interface for the protobuf available in 5 Golang:

在使用之前,我们先了解一下每个 Protobuf 消息在 Golang 中有哪一些可用的接口:    1.  每一个 Protobuf 消息对应一个 Golang 结构体    2. 消息中域名字为 camel_case 在对应的 Golang 结构体中为 CamelCase    3. 消息对应的 Golang 结构体中不存在 setter 方法,只需要直接对结构体赋值即可,赋值时可能使用到一些辅助函数,例如:       msg.Foo = proto.String("hello")    4. 消息对应的 Golang 结构体中存在 getter 方法,用于返回域的值,如果域未设置值,则返回一个默认值    5. 消息中非 repeated 的域都被实现为一个指针,指针为 nil 时表示域未设置消息中 repeated 的域被实现为 slice    6. 访问枚举值时,使用“枚举类型名_枚举名”的格式(更多内容可以直接阅读生成的源码)    7. 使用 proto.Marshal 函数进行编码,使用 proto.Unmarshal 函数进行解码

Reference Links:

    1. Using Protobuf in Golang
    2. Golang C + + installation
    3. Go language use 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.