mac 下配置 protobuf golang插件 并使用
介绍
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 的语法是不一样的
安装 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
编译过程需要 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 名字叫做 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())
}
}
测试运行一下,如果出现问题或者代码有误,请自行解决一下~~

mac 下配置 protobuf golang插件 并使用的更多相关文章
- mac 下配置protobuf 3.0 golang环境
protobuf 3.0 与 之前的 protobuf 2.6 的语法是不一样的.需要重新安装一下,本机的环境是 OS X Yosemite 10.10.2 1. 不采用home brew安装,用 ...
- mac下配置gdb调试golang
mac下配置gdb调试golang 原文链接 https://sourceware.org/gdb/wiki/BuildingOnDarwin Building GDB for Darwin Crea ...
- 【高可用HA】Apache (4) —— Mac下配置Apache Httpd负载均衡(Load Balancer)之mod_jk
Mac下配置Apache Httpd负载均衡(Load Balancer)之mod_jk httpd版本: httpd-2.4.17 jk版本: tomcat-connectors-1.2.41 参考 ...
- 【高可用HA】Apache (3) —— Mac下配置Apache Httpd负载均衡(Load Balancer)之mod_proxy
Mac下配置Apache Httpd负载均衡(Load Balancer)之mod_proxy httpd版本: httpd-2.4.17 参考来源: Apache (1) -- Mac下安装Apac ...
- mac下配置开发环境
常用命令 显示隐藏文件 1 defaults write com.apple.finder AppleShowAllFiles -boolean true ; killall Finder 关闭隐藏文 ...
- 在Mac下配置php开发环境:Apache+php+MySql
/private/etc/apache2/httpd.conf 一.启动Apache sudo apachectl start sudo apachectl -v 可以查看到Apache的版本信息 ...
- iOS开发MAC下配置svn
版本控制对于团队合作显得尤为重要,那么如何在iOS开发中进行版本控制呢?在今天的博客中将会介绍如何在MAC下配置SVN服务器,如何导入我们的工程,如何在Xcode中进行工程的checkOut和Comm ...
- mac下配置openfire
下载 在浏览器中打开如下网址http://www.igniterealtime.org/downloads/index.jsp,根据你的操作系统选择对应的版本进行下载,这里我是在mac下配置的,所以选 ...
- Mac下配置idk
Mac下配置java #以下进入啰嗦模式演示添加jdk7 #下载jdk7的mac版 #官网下载地址http://www.oracle.com/technetwork/java/javase/downl ...
随机推荐
- BZOJ 2687: 交与并
答案存在于 1.两个互相包含的区间 2.两个互不包含的区间 决策单调性显然 但是这代码很精妙啊,并不知道这个为什么能这样写 #include<cstdio> #include<alg ...
- Ubuntu超简单文书编辑器:nano
nano 的使用很简单,可以直接加上档名就能够开启一个旧档或新档! 直接在终端输入指令:nano text.txt,如下图所示打开的是已有的文档! 第一部分反白部分,是nano的版本与档名 第二部分可 ...
- day18-socket 编程
1.Socket是网络上的使用的交互信息得方法,也叫套接字 用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. 通讯原理 Soc ...
- javascript 内置日期转换方法
var d = new Date(); console.log(d); // 输出:Mon Nov 04 2013 21:50:33 GMT+0800 (中国标准时间) console.log(d.t ...
- [POJ 1008] Maya Calendar C++解题
Maya Calendar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 62297 Accepted: 192 ...
- WordCount 2.0(结对项目)
序言 合作伙伴 201631062220 201631062120 项目码云地址: https://gitee.com/zhege/WordCount 作业详细要求 系统分析与设计结对项目 ...
- 刷题总结——regular words(hdu1502 dp+高精度加法+压位)
题目: Problem Description Consider words of length 3n over alphabet {A, B, C} . Denote the number of o ...
- jenkins项目数据位置
JENKINS_HOME: C:\Documents and Settings\AAA\.jenkins /root/.jenkins jenkins迁移只需要备份JENKINS_HOME的内容即可 ...
- 【CF387D】George and Interesting Graph(二分图最大匹配)
题意:给定一张n点m边没有重边的有向图,定义一个有趣图为:存在一个中心点满足以下性质: 1.除了这个中心点之外其他的点都要满足存在两个出度和两个入度. 2.中心 u 需要对任意顶点 v(包括自己)有一 ...
- hdu 4939 2014 Multi-University Training Contest 7 1005
Stupid Tower Defense Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/ ...