原生的protoc不包含go插件,protoc-gen-go是protobuf编译插件的go版本。
1 下载源码
git clone https://github.com/golang/protobuf
也可浏览器方式上https://github.com/golang/protobuf下载。
2 进入protoc-gen-go源码目录
cd protobuf/protoc-gen-go
3 编译生成protoc-gen-go
go build -o protoc-gen-go main.go
4 复制protoc-gen-go到bin目录
cp protoc-gen-go /usr/local/go/bin
5 使用示例
protoc --go_out=plugins=grpc:. hello.proto
如果编译.proto文件时找不到protoc-gen-go,则在编译时会遇到如下错误:
$protoc --go_out=plugins=grpc:. hello.proto
protoc-gen-go: program not found or is not executable
Please specify a program using absolute path or make sure the program is available in your PATH system variable
--go_out: protoc-gen-go: Plugin failed with status code 1.