您的位置:首页 > 编程语言 > Go语言

mac 下配置 protobuf golang插件 并使用

2017-04-28 14:10 841 查看

介绍

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

可以用于结构化数据串行化,或者说序列化。

它很适合做数据存储或 RPC 数据交换格式。

可用于通讯协议、数据存储等领域的语言无关、平台无关、可扩展的序列化结构数据格式。

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

LanguageSource
C++ (include C++ runtime and protoc)src
Javajava
Pythonpython
Objective-Cobjectivec
C#csharp
JavaNanojavanano
JavaScriptjs
Rubyruby
Gogolang/protobuf
PHPallegro/php-protobuf
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 protbuf 2.6.1

安装 3.0 版本

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

可以选择让brew安装开发版

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

-Windows protobuf 3.0.2

brew tap 安装

http://brewformulas.org/Protobuf

➜  ~ brew tap homebrew/versions
➜  ~ brew info protobuf
protobuf: stable 3.0.2 (bottled), HEAD
Protocol 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:43
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/protobuf.rb ==> Dependencies
Build: 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
==> Caveats
Editor support and examples have been installed to:
/usr/local/Cellar/protobuf/3.0.2/share/doc/protobuf

➜  ~brew install protobuf

编译安装

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

下载源码 https://github.com/google/protobuf

protobuf release tag 3.0.0

编译过程需要
gtest


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

检查安装结果

protoc  --version

安装golang for protobuf插件

需要

proto

protoc-gen-go

go get -u -v github.com/golang/protobuf/proto
go 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 main

import (
"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())
}
}

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: