最近的项目中,一直使用Json作数据传输。Json用起来的确很方便。但相对于protobuf数据量更大些。作一个移动端应用,为用户省点流量仍是颇有必要的。正好也能够学习一下protobuf的使用linux
跟Json相比protobuf性能更高,更加规范git
编解码速度快,数据体积小github
使用统一的规范,不用再担忧大小写不一样致使解析失败等蛋疼的问题了golang
但也失去了一些便利性shell
改动协议字段,须要从新生成文件。windows
数据没有可读性服务器
在go中使用protobuf,有两个可选用的包goprotobuf(go官方出品)和gogoprotobuf。
gogoprotobuf彻底兼容google protobuf,它生成的代码质量和编解码性能均比goprotobuf高一些tcp
首先去https://github.com/google/pro... 上下载protobuf的编译器protoc,windows上能够直接下到exe文件(linux则须要编译),最后将下载好的可执行文件拷贝到$GOPATH的bin目录下($GOPATH/bin目录最好添加到系统环境变量里)性能
go get github.com/golang/protobuf/proto
go get github.com/golang/protobuf/protoc-gen-go
protoc --go_out=. *.proto
gogoprotobuf有两个插件可使用学习
protoc-gen-gogo:和protoc-gen-go生成的文件差很少,性能也几乎同样(稍微快一点点)
protoc-gen-gofast:生成的文件更复杂,性能也更高(快5-7倍)
//gogo go get github.com/gogo/protobuf/protoc-gen-gogo //gofast go get github.com/gogo/protobuf/protoc-gen-gofast
go get github.com/gogo/protobuf/proto go get github.com/gogo/protobuf/gogoproto //这个不装也不要紧
//gogo protoc --gogo_out=. *.proto //gofast protoc --gofast_out=. *.proto
这里只是简单的用go test测试了一下
//goprotobuf "编码":447ns/op "解码":422ns/op //gogoprotobuf-go "编码":433ns/op "解码":427ns/op //gogoprotobuf-fast "编码":112ns/op "解码":112ns/op
syntax = "proto3"; //指定版本,必需要写(proto三、proto2) package proto; enum FOO { X = 0; }; //message是固定的。UserInfo是类名,能够随意指定,符合规范便可 message UserInfo{ string message = 1; //消息 int32 length = 2; //消息大小 int32 cnt = 3; //消息计数 }
package main import ( "bufio" "fmt" "net" "os" stProto "proto" "time" //protobuf编解码库,下面两个库是相互兼容的,可使用其中任意一个 "github.com/golang/protobuf/proto" //"github.com/gogo/protobuf/proto" ) func main() { strIP := "localhost:6600" var conn net.Conn var err error //链接服务器 for conn, err = net.Dial("tcp", strIP); err != nil; conn, err = net.Dial("tcp", strIP) { fmt.Println("connect", strIP, "fail") time.Sleep(time.Second) fmt.Println("reconnect...") } fmt.Println("connect", strIP, "success") defer conn.Close() //发送消息 cnt := 0 sender := bufio.NewScanner(os.Stdin) for sender.Scan() { cnt++ stSend := &stProto.UserInfo{ Message: sender.Text(), Length: *proto.Int(len(sender.Text())), Cnt: *proto.Int(cnt), } //protobuf编码 pData, err := proto.Marshal(stSend) if err != nil { panic(err) } //发送 conn.Write(pData) if sender.Text() == "stop" { return } } }
package main import ( "fmt" "net" "os" stProto "proto" //protobuf编解码库,下面两个库是相互兼容的,可使用其中任意一个 "github.com/golang/protobuf/proto" //"github.com/gogo/protobuf/proto" ) func main() { //监听 listener, err := net.Listen("tcp", "localhost:6600") if err != nil { panic(err) } for { conn, err := listener.Accept() if err != nil { panic(err) } fmt.Println("new connect", conn.RemoteAddr()) go readMessage(conn) } } //接收消息 func readMessage(conn net.Conn) { defer conn.Close() buf := make([]byte, 4096, 4096) for { //读消息 cnt, err := conn.Read(buf) if err != nil { panic(err) } stReceive := &stProto.UserInfo{} pData := buf[:cnt] //protobuf解码 err = proto.Unmarshal(pData, stReceive) if err != nil { panic(err) } fmt.Println("receive", conn.RemoteAddr(), stReceive) if stReceive.Message == "stop" { os.Exit(1) } } }