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 ...
随机推荐
- JS实现——Base64编码解码,带16进制显示
在网上找了个JS实现的Base64编码转换,所以就想自己研究下,界面如下: 将代码以BASE64方式加密.解密 请输入要进行编码或解码的字符: 编码结果以ASCII码16进制显示 解码结果以ASCII ...
- js各种继承方式和优缺点的介绍
js各种继承方式和优缺点的介绍 作者: default 参考网址2 写在前面 本文讲解JavaScript各种继承方式和优缺点. 注意: 跟<JavaScript深入之创建对象>一样,更像 ...
- 不可取代的网站开发工具---------dreamweaver
现在web开发的工具越来越多,sublime text,webstorm等web开发工具日益崛起,一直威胁着当年网页三剑客之一的dreamweaver工具的地位,然而dreamweaver却是无法取代 ...
- 《程序员思维修炼》读书笔记——week4
<程序员思维修炼>读书笔记——week4 PB16061441 陈昶金 这周读的是Andy Hunt的著作<程序员思维修炼>,这本书对于我这种刚刚入门的新手很友好,大多是讲一些 ...
- hdu2094 stl之set的应用
产生冠军 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- POJ 1952
BUY LOW, BUY LOWER Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 7748 Accepted: 267 ...
- Linux Shell系列教程之(一)Shell简介
本文是Linux Shell系列教程的第(一)篇,更多shell教程请看:Linux Shell系列教程 想要学习linux,shell知识必不可少,今天就给大家来简单介绍下shell的基本知识. S ...
- 漫谈DNS
文章作者:luxianghao 文章来源:http://www.cnblogs.com/luxianghao/p/6189633.html 转载请注明,谢谢合作. 免责声明:文章内容仅代表个人观点, ...
- C++之Effective STL学习笔记Item7
假设我们现在有以下代码: void doSomething() { vector<Widget*> vwp; ; i < SOME_MAGIC_NUMBER; ++i) vwp.pu ...
- Java面试题之ArrayList和LinkedList的区别
先看下类图: 相同点: 都实现了List接口和Collection: 不同点: 1.ArrayList是基于数组实现的:LinkedList是基于链表实现的: 2.ArrayList随机查询速度快:L ...