1.查看brew当前维护的protobuf版本有哪些
huoxun@huoxundeMacBook-Pro ~ % brew search protobuf
==> Formulae
protobuf ✔ protobuf-c protobuf@21 protobuf@3 ✔ swift-protobuf
==> Casks
protopie
2.安装指定版本的protobuf
huoxun@huoxundeMacBook-Pro ~ % brew install protobuf@3
3.查看protobuf安装的版本,能显示版本说明安装成功
huoxun@huoxundeMacBook-Pro ~ % protoc --version
libprotoc 3.20.3
4.下载protobuf的golang插件,安装protoc-gen-go,用来将.proto文件编译成golang代码
huoxun@huoxundeMacBook-Pro ~ % go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go: downloading google.golang.org/protobuf v1.31.0
5.使用实例
-
创建一个新的项目,新建一个.proto文件
syntax = "proto3"; package main; option go_package ="./;main;"; //第一个参数是输出路径,第二个参数是输出go文件包名 message User { int64 user_id = 1; string user_name = 2; string password = 3; } -
将.proto文件编译成go语言文件
uoxun@huoxundeMacBook-Pro test % protoc ./test.proto --go_out=./
6.可能出现的错误
-
no matches found: *
在~/.zshrc中加上:setopt no_nomatch
-
Missing input file.
如果.proto在当前目录,要以"./"开始
-
安装错误版本protobuf,这样卸载protobuf
brew uninstall protobuf或者再安装指定版本,根据提示更改。