mac 下配置 protobuf golang插件 并使用 您所在的位置:网站首页 golang插件化 mac 下配置 protobuf golang插件 并使用

mac 下配置 protobuf golang插件 并使用

#mac 下配置 protobuf golang插件 并使用| 来源: 网络整理| 查看: 265

介绍

Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准Protocol Buffers 是一种轻便高效的结构化数据存储格式

可以用于结构化数据串行化,或者说序列化。 它很适合做数据存储或 RPC 数据交换格式。 可用于通讯协议、数据存储等领域的语言无关、平台无关、可扩展的序列化结构数据格式。

支持语言很多,C++ java python php golang 等,支持列表

Language Source C++ (include C++ runtime and protoc) Java Python Objective-C C# JavaNano JavaScript Ruby Go PHP

protobuf 3.0 与 之前的 protobuf 2.6 的语法是不一样的

安装 ProtoBuf 安装 2.6 # 查看protobuf信息brew info protobuf# 安装brew install protobuf # 检查安装结果 protoc --version libprotoc 2.6.1

Linux 请查询 apt-get or yum

Windows 

安装 3.0 版本

因为3.0在开发中,不能直接使用brew安装稳定版

可以选择让brew安装开发版 可以选择编译安装开发版本,编译过程需要自备梯子

-Windows 

brew tap 安装

➜  ~ brew tap homebrew/versions➜  ~ brew info protobufprotobuf: stable 3.0.2 (bottled), HEADProtocol buffers (Google's data interchange format)https://github.com/google/protobuf//usr/local/Cellar/protobuf/2.6.1 (121 files, 6.9M) *  Poured from bottle on 2016-09-07 at 12:08:43From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/protobuf.rb==> DependenciesBuild: autoconf ✔, automake ✔, libtool ✔==> Options--c++11   Build using C++11 mode--universal   Build a universal binary--with-test   Run build-time check--without-python   Build without python support--HEAD   Install HEAD version==> CaveatsEditor support and examples have been installed to:  /usr/local/Cellar/protobuf/3.0.2/share/doc/protobuf➜  ~brew install protobuf 编译安装

因为3.0在开发中,不能直接使用brew安装,需要编译,编译过程需要自备梯子

下载源码 

编译过程需要 gtest

brew info automakebrew info libtool# 没有这两个就安装./autogen.sh# 检查没问题了./configuremake -j4make check make install

检查安装结果

protoc  --version 安装golang for protobuf插件

需要

go get -u -v github.com/golang/protobuf/protogo get -u -v github.com/golang/protobuf/protoc-gen-go

请将你的$GOPATH/bin 设置为环境变量,这样才可以使用protoc-gen-go

使用protobuf

说明:本用例是在protobuf version 2.6.1中执行

编写 proto 文件

使用文本编辑器编辑文件 Im.helloworld.proto,内容为

请认真对待 proto 文件的文件名,常用规则packageName.MessageName.proto

package Im; enum FOO { X = 17; }; message helloworld { required int32 id = 1; // Id required string str = 2; // Str optional int32 opt = 3; // Opt optional field }

解释这个文本

package 名字叫做 Im 定义了一个消息 helloworld 该消息有三个成员,类型为 int32 的 id,另一个为类型为 string 的成员 str。opt 是一个可选的成员,即消息中可以不包含该成员 编译 .proto 文件 protoc --go_out=. Im.helloworld.proto# 编译当前目录下所有的proto文件protoc --go_out=. *.proto

出现错误提示,请检查上面的安装过程

生成的文件为 Im.helloworld.pb.go

内容主体有

const ( FOO_X FOO = 17)type Helloworld struct { Id   *int32  `protobuf:"varint,1,req,name=id" json:"id,omitempty"` Str  *string `protobuf:"bytes,2,req,name=str" json:"str,omitempty"` Opt *int32 `protobuf:"varint,3,opt,name=opt" json:"opt,omitempty"` XXX_unrecognized []byte `json:"-"` } 测试这个生成代码

编写测试代码

package mainimport ("github.com/golang/protobuf/proto""example""fmt" ) func main() { // 创建一个消息 Info info := &example.Helloworld{ Id: proto.String("hello"), Str: proto.Int32(17), } // 进行编码 data, err := proto.Marshal(info) if err != nil { fmt.Printf("marshaling error: ", err) } // 进行解码 newInfo := &example.Helloworld{} err = proto.Unmarshal(data, newInfo) if err != nil { fmt.Printf("unmarshaling error: ", err) } if info.GetId() != newInfo.GetId() { fmt.Printf("data mismatch %q != %q", info.GetId(), newInfo.GetId()) } }

测试运行一下,如果出现问题或者代码有误,请自行解决一下~~

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有