mac 下配置 protobuf golang 插件 并使用

[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

編譯安裝過程省略

文檔 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 !

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 轉(zhuǎn)自:http://www.th7.cn/Program/IOS/201506/484001.shtml 首先是怎...
    Aiya閱讀 15,169評論 2 8
  • 由于工程項目中擬采用一種簡便高效的數(shù)據(jù)交換格式,百度了一下發(fā)現(xiàn)除了采用 xml、JSON 還有 ProtoBuf(...
    黃海佳閱讀 49,181評論 1 23
  • Protocol Buffers 是 Google 出品的用來序列化/反序列化數(shù)據(jù)的工具。原生支持 C++、Jav...
    Mr_小黑君閱讀 4,107評論 1 1
  • 課程即將結(jié)束,下周的現(xiàn)在我終于不用天天來回跑了。辦輔導(dǎo)班一個月,有得有失。差不多有一個月沒寫文章了,因為我找了一個...
    魅格體閱讀 296評論 0 0
  • 爐火待純青,諸法空相是。無智亦無得,名菩提薩綞。非人眾壽者,假立虛擬之。時空隧道兮,見相無痕時
    真如自在閱讀 319評論 1 2

友情鏈接更多精彩內(nèi)容