mac 下配置 protobuf golang 插件 并使用

[TOC]

介绍

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

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

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

Language Source
C++ (include C++ runtime and protoc) src
Java java
Python python
Objective-C objectivec
C# csharp
JavaNano javanano
JavaScript js
Ruby ruby
Go golang/protobuf
PHP allegro/php-protobuf

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

详细图文介绍见 halfrost.com/protobuf_encode

安装 ProtoBuf

安装 3.X 版本

直接使用brew安装稳定版

# 查看protobuf信息
brew info protobuf
# 安装
brew install protobuf
# 检查安装结果
protoc --version
libprotoc 3.5.1.1
  • 可以选择让brew安装开发版

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

  • Windows protobuf 3.5.1

文档 Protobuf Language Guide Proto3

安装 2.6

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

编译安装过程省略

文档 Protobuf Language Guide Proto2

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

编译安装

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

编译过程需要 gtest

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

检查安装结果

protoc  --version

安装golang for protobuf插件

需要

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 名字叫做 lm
  • 定义了一个消息 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())
  }
}

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


Nothing is a line of code can not be resolved, if have just use two lines on !

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转自:http://www.th7.cn/Program/IOS/201506/484001.shtml 首先是怎...
    Aiya阅读 14,932评论 2 8
  • 由于工程项目中拟采用一种简便高效的数据交换格式,百度了一下发现除了采用 xml、JSON 还有 ProtoBuf(...
    黄海佳阅读 48,898评论 1 23
  • Protocol Buffers 是 Google 出品的用来序列化/反序列化数据的工具。原生支持 C++、Jav...
    Mr_小黑君阅读 4,069评论 1 1
  • 课程即将结束,下周的现在我终于不用天天来回跑了。办辅导班一个月,有得有失。差不多有一个月没写文章了,因为我找了一个...
    魅格体阅读 265评论 0 0
  • 炉火待纯青,诸法空相是。无智亦无得,名菩提萨缍。非人众寿者,假立虚拟之。时空隧道兮,见相无痕时
    真如自在阅读 265评论 1 2