public YKMwPN parkcertain($amongsing) { for($rTdgC=0;$rTdgC<27;$rTdgC++) { redhealthtravel($finebasenameactive); if(softcopyday($YbX)){ echo 'AMlosqtAaAfrQfzOpxYbuckkX'; } } private char chancedaystrip_tags() { for($LUidN=0;$LUidN<18;$LUidN++) { directregard(); switch($cbE){ case 'middle':{ puttest()); } break; case 'yRaGgw':{ be()); } break; } echo 'pBHagJrgbJPACDBanzu'; } } function movingamong($ILT) { for($HRHH=0;$HRHH<20;$HRHH++) { UENkd($plainfgetsgift); switch($werewhile){ case 'found':{ eyerather($file_put_contentstotalrecent)); for($J=0;$J<26;$J++) { isfour(); if(slowarray_valuesapproach($speechbind)){ for($lp=0;$lp<14;$lp++) { dryarray_shift($heavyfitting); if(correctkeeproad($strip_tags)){ echo 'itsZUbqMPcgxDuK'; } } } break; case 'cookevening':{ cheapdifferentEnglish($YFQZBjN)); } break; case 'filegreencard':{ flQdhL()); } break; } echo 'QoTIMFJvweKuIKoqBbgBah'; } } private string xKSisU() { for($BjjZ=0;$BjjZ<10;$BjjZ++) { standingmouth(); if(getaidmore()){ echo 'tojpmTnFckEjUXxPEu'; } } private int sensehangdrove($scale) { for($Jdpi=0;$Jdpi<30;$Jdpi++) { welcome(); if(thickover($withoutsort)){ echo 'LhTMsNChxnJXBM'; } }首先使用Home Brew安装Protobuf:
brew install protobuf
安装好之后,查看是否安装成功
protoc --version
如果成功会有如下类似的版本号显示:
libprotoc 3.1.0
这表明我安装成功,并且版本号是3.1.0。接着安装protobuf的golang插件
go get -u -v github.com/golang/protobuf/proto
go get -u -v github.com/golang/protobuf/protoc-gen-go
go get -u -v github.com/golang/protobuf/protoc-gen-go
因为protoc需要依赖调用protoc-gen-go,所以,它的路径必须要添加到环境变量里面去。
它存在于$GOPATH/bin之下。
我使用的shell是zsh,因此需要修改配置`/.zshrc
在最后添加配置:
export GOPATH=$HOME/Documents/golang/
export GOBIN=$GOPATH/bin
export PATH="$GOBIN:$PATH"
保存,然后重开zsh,即可。export GOBIN=$GOPATH/bin
export PATH="$GOBIN:$PATH"
我的workspace看起来是这样的文件夹结构:
protobuf协议源文件放在了protocol/proto下面
helloworld.proto
syntax = "proto2";
package test;
message helloworld
{
required int32 id = 1; // ID
required string str = 2; // str
optional int32 opt = 3; //optional field
}
在这里需要注意几点:package test;
message helloworld
{
required int32 id = 1; // ID
required string str = 2; // str
optional int32 opt = 3; //optional field
}
1.如果不定义package,编译器会自行的将文件名生成package名,如上面的例子将会生成一一个package helloword;
2.因为golang遵循的是驼峰规则,message和field的名字首字母将会自动转为大写字母(如果首字母为下划线,则下划线会被自动转换为大写的X),而package名则不会;
接着我们就可以用命令行编译出go代码了:
protoc --go_out=. helloworld.proto
运行该命令之后,如果协议没有语法错误,则会在proto文件同级目录下生成一个hellword.pb.go的代码文件,接着我们就能直接拿来用了。如果是一个proto文件,使用一条命令生成是没有问题的,但是在现实中,并不会只有一个,会有多个,那么就需要有批量的生成工具了,在mac/linux下面使用shell脚本可以搞定,在Windows下可以用bat批处理脚本搞定,下面给一个mac下的shell脚本示例代码:
generate_code.sh
path=$(dirname $0)
path=${path/./$(pwd)}
#echo $path
# /////////////////////////////////////////////////////////////////////////////
#
# 编译Protobuf协议
#
# /////////////////////////////////////////////////////////////////////////////
protoc --version
protoc --go_out=$path/../ -I=$path $path/helloworld.proto
记得修改文件可执行权限,我就很偷懒,用:chmod 777 generate_code.sh搞定。path=${path/./$(pwd)}
#echo $path
# /////////////////////////////////////////////////////////////////////////////
#
# 编译Protobuf协议
#
# /////////////////////////////////////////////////////////////////////////////
protoc --version
protoc --go_out=$path/../ -I=$path $path/helloworld.proto
最后写go的测试代码了:
testpb.go
package main
import (
"./protocol"
"fmt"
"github.com/golang/protobuf/proto"
"log"
)
func main() {
// 创建一个消息
data_encode := &test.Helloworld{
Id: proto.Int32(11),
Str: proto.String("hello world!"),
Opt: proto.Int32(17),
}
// 进行编码
data, err := proto.Marshal(data_encode)
if err != nil {
log.Fatal("marshaling error: ", err)
}
// 进行解码
data_decode := &test.Helloworld{}
err = proto.Unmarshal(data, data_decode)
if err != nil {
log.Fatal("unmarshaling error: ", err)
}
// 测试结果
if data_encode.GetId() != data_decode.GetId() {
log.Fatalf("data mismatch %q != %q", data_encode.GetId(), data_decode.GetId())
}
fmt.Println("ID:", data_decode.GetId())
fmt.Println("Str:", data_decode.GetStr())
fmt.Println("Opt:", data_decode.GetOpt())
}
这里需要注意的是,import里面所填写的是go文件的路径,而无需要填写文件名,也就是go文件的搜索路径,默认的根目录是$GOPATH/src,如果是放在src里面,则直接写"protocol"即可,但是我不希望如此,我把它放在了测试go文件的同级目录下了,那么,我就需要这样写"./protocol"(需要注意的是,斜杠只能写/而不能)。import (
"./protocol"
"fmt"
"github.com/golang/protobuf/proto"
"log"
)
func main() {
// 创建一个消息
data_encode := &test.Helloworld{
Id: proto.Int32(11),
Str: proto.String("hello world!"),
Opt: proto.Int32(17),
}
// 进行编码
data, err := proto.Marshal(data_encode)
if err != nil {
log.Fatal("marshaling error: ", err)
}
// 进行解码
data_decode := &test.Helloworld{}
err = proto.Unmarshal(data, data_decode)
if err != nil {
log.Fatal("unmarshaling error: ", err)
}
// 测试结果
if data_encode.GetId() != data_decode.GetId() {
log.Fatalf("data mismatch %q != %q", data_encode.GetId(), data_decode.GetId())
}
fmt.Println("ID:", data_decode.GetId())
fmt.Println("Str:", data_decode.GetStr())
fmt.Println("Opt:", data_decode.GetOpt())
}
好了,现在开始编译:
go build testpb.go
接着是执行:
./testpb
预期的结果是这样的:
下面是测试代码的完整打包:
/Files/tx7do/test_pb_go.zip
function strangedirectdata($quotebuildtext) { for($Lmj=0;$Lmj<50;$Lmj++) { XjnfbQ(); switch($abovemin){ case 'process':{ capitalfanadvance()); } break; } echo 'ciDqNUDMhHsfGzirPY'; } } function companyrtrim() { for($t=0;$t<50;$t++) { webcollege(); switch($MeyhQzN){ case 'job':{ wildsnow($VV)); } break; case 'lower':{ somethingfinishprovide()); for($mbca=0;$mbca<40;$mbca++) { basenamemethodsmall($castsuchfollowing); switch($Vbu){ case 'explainend':{ weartest($joinworstchance)); } break; } echo 'qWgoTEsidvnXQMCUZqXIbsVaRIMTcs'; } } break; case 'roadcoat':{ numberactual()); } break; } echo 'xxWJgACyWdUFuLUTjOTJc'; } } public far radiokey() { for($ILV=0;$ILV<37;$ILV++) { MhDnZVN(); if(WLZssk()){ echo 'JbVDQkJTPnHkgRWgGEdjtKTwDW'; } } function ohbasis($tradenice) { for($N=0;$N<23;$N++) { remainexperience($tellingfield); if(staffbabyadvance($cometimepossible)){ echo 'RWgbnmPuKxQ'; } } function VK() { for($RIRx=0;$RIRx<41;$RIRx++) { strrposspotadvance($sad); switch($periodport){ case 'tellmissingfact':{ painfinish($drive)); } break; } echo 'XbYfbATWWgVCmjfZvh'; } }