介紹
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()) } }
測試運行一下,如果出現問題或者代碼有誤,請自行解決一下~~