编译安装编译器protoc
wget https://github.com/protocolbuffers/protobuf/releases/download/v2.6.1/protobuf-2.6.1.zip
unzip protobuf-2.6.1.zip
cd protobuf-2.5.0
./configure CC=clang CXX=clang++ CXXFLAGS='-std=c++11 -stdlib=libc++ -O3 -g' LDFLAGS='-stdlib=libc++' LIBS="-lc++ -lc++abi"
make -j 4
sudo make install
protoc --version
安装插件
go get -u github.com/golang/protobuf/protoc-gen-go
定义消息类型
syntax = "proto3";
package main;
// this is a comment
message Student {
string name = 1;
bool male = 2;
repeated int32 scores = 3;
}
在当前目录下执行:
protoc --go_out=. *.proto
ls
# student.pb.go student.proto
参考:
https://gist.github.com/liushooter/44bd8e2f6f277f2631841787aafe3d9d
https://geektutu.com/post/quick-go-protobuf.html
https://colobu.com/2019/10/03/protobuf-ultimate-tutorial-in-go/
https://www.cnblogs.com/sanshengshui/p/9739521.html
有疑问加站长微信联系(非本文作者)