[TOC]
介紹
Google Protocol Buffer( 簡稱 Protobuf) 是 Google 公司內(nèi)部的混合語言數(shù)據(jù)標準
Protocol Buffers 是一種輕便高效的結(jié)構(gòu)化數(shù)據(jù)存儲格式
- 可以用于結(jié)構(gòu)化數(shù)據(jù)串行化,或者說序列化。
- 它很適合做數(shù)據(jù)存儲或 RPC 數(shù)據(jù)交換格式。
- 可用于通訊協(xié)議、數(shù)據(jù)存儲等領(lǐng)域的語言無關(guān)、平臺無關(guān)、可擴展的序列化結(jié)構(gòu)數(shù)據(jù)格式。
支持語言很多,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安裝穩(wěn)定版
# 查看protobuf信息
brew info protobuf
# 安裝
brew install protobuf
# 檢查安裝結(jié)果
protoc --version
libprotoc 3.5.1.1
可以選擇讓brew安裝開發(fā)版
可以選擇編譯安裝開發(fā)版本,編譯過程需要自備梯子
Windows protobuf 3.5.1
文檔 Protobuf Language Guide Proto3
安裝 2.6
下載源碼 https://github.com/google/protobuf
protobuf release tag 2.6.1
- Windows protbuf 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
檢查安裝結(jié)果
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設(shè)置為環(huán)境變量,這樣才可以使用protoc-gen-go
使用protobuf
說明:本用例是在protobuf version 2.6.1中執(zhí)行
編寫 proto 文件
使用文本編輯器編輯文件 Im.helloworld.proto,內(nèi)容為
請認真對待 proto 文件的文件名,常用規(guī)則
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
出現(xiàn)錯誤提示,請檢查上面的安裝過程
生成的文件為 Im.helloworld.pb.go
內(nèi)容主體有
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() {
// 創(chuàng)建一個消息 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())
}
}
測試運行一下,如果出現(xiàn)問題或者代碼有誤,請自行解決一下~~
Nothing is a line of code can not be resolved, if have just use two lines on !