This is a creation in Article, where the information may have evolved or changed.
[TOC]
Introduced
Google Protocol Buffer (protobuf) is a mixed-language data standard within Google Inc.
Protocol buffers is a lightweight and efficient structured data storage format
- Can be used for structured data serialization, or serialized.
- It is ideal for data storage or RPC data interchange formats.
- It can be used in the communication protocol, data storage and other fields of the language-independent, platform-independent, extensible serialization structure data format.
Support a lot of languages, C + + java python php Golang, etc., support list
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 is not the same as the previous PROTOBUF 2.6 syntax
Installing PROTOBUF
Installation 2.6
# 查看protobuf信息brew info protobuf# 安装brew install protobuf# 检查安装结果protoc --versionlibprotoc 2.6.1
Linux Please inquireapt-get or yum
Windows Protbuf 2.6.1
Install version 3.0
Since 3.0 is in development, you cannot use brew to install the stable version directly
- You can choose to have brew install the development version
- can choose to compile and install the development version, the compilation process needs to bring a ladder
-windows Protobuf 3.0.2
Brew TAP Installation
Http://brewformulas.org/Protobuf
➜ ~ brew tap homebrew/versions➜ ~ brew info protobufprotobuf: stable 3.0.2 (bottled), HEADProtocol 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:43From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/protobuf.rb==> DependenciesBuild: 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==> CaveatsEditor support and examples have been installed to: /usr/local/Cellar/protobuf/3.0.2/share/doc/protobuf➜ ~brew install protobuf
Compiling the installation
Because 3.0 in development, can not directly use brew installation, need to compile, compile process need to own ladder
Download Source Https://github.com/google/protobuf
PROTOBUF Release Tag 3.0.0
The compilation process requiresgtest
brew info automakebrew info libtool# 没有这两个就安装./autogen.sh# 检查没问题了./configuremake -j4make checkmake install
Check the installation results
protoc --version
Installing the Golang for PROTOBUF plugin
Need
go get -u -v github.com/golang/protobuf/protogo get -u -v github.com/golang/protobuf/protoc-gen-go
Please $GOPATH/bin
set your environment variable so that you can use the Protoc-gen-go
Using Protobuf
Description: This use case is performed in PROTOBUF version 2.6.1
Writing proto Files
Use a text editor to edit the file with the Im.helloworld.proto
content
Please take seriously the file name of the proto file, common rulespackageName.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 }
Explain this text
- The package is named Im
- Defines a message HelloWorld
- The message has three members, the ID of type int32, and the other is a member of type string str. Opt is an optional member, that is, the message may not contain the Member
Compiling the. Proto file
protoc --go_out=. Im.helloworld.proto# 编译当前目录下所有的proto文件protoc --go_out=. *.proto
An error message appears, please check the installation procedure above
The resulting file isIm.helloworld.pb.go
Content body has
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:"-"`}
Test this build code
Writing test Code
package mainimport ( "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()) }}
Test run, if there is a problem or code is wrong, please resolve it yourself ~ ~
Nothing was a line of code can isn't resolved, if has just use of the lines on!