尝试安装protobuf,并生成golang代码。记录一下安装过程中的踩坑。
事后感觉全都是不看提示的错 wwwww。
按照教程,指令如下
go get -u github.com/golang/protobuf/protoc-gen-go
但执行后会有如下错误
go: module github.com/golang/protobuf is deprecated: Use the "google.golang.org/protobuf" module instead.
go get: installing executables with 'go get' in module mode is deprecated.
Use 'go install pkg@version' instead.
For more information, see https://golang.org/doc/go-get-install-deprecation
or run 'go help get' or 'go help install'.
报了两个错误
现在想要拉取protoc-gen-go需要去google.golang.org/protobuf拉取,原来的路径已经废弃了。我使用的go版本是1.17。而Go1.17版使用go install安装依赖。所以应该按照它下面的格式go install pkg@version进行拉取。所以拉取命令如下
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
编译.proto文件
创建一个文件夹helloproto
,创建文件student.prto
mkdir helloproto
cd helloproto
vim student.proto
student.proto
文件如下
syntax = "proto3";
package helloproto;
option go_package =".";
message Student {
string name = 1;
bool male = 2;
repeated int32 scores = 3;
}
此时目录结构如下
按照教程,执行以下命令
protoc --go_out=. *.proto
报以下错误
protoc-gen-go: program not found or is not executable
原因是因为当前路径没有在环境变量中,便进行如下设置
vim ~/.bashrc
并在文件下方添加
export PATH = $PATH:$GOPATH/bin
注意保存并生效
source ~/.bashrc
这里保存之后还需要关闭命令行或者新开一个命令行,我一直在原来的命令行 *** 作好像一直没有生效。。换了个命令行就可以了
生效后再次执行protoc --go_out=. *.proto
报以下错误
protoc-gen-go: unable to determine Go import path for "student.proto"
Please specify either:
• a "go_package" option in the .proto source file, or
• a "M" argument on the command line.
See https://developers.google.com/protocol-buffers/docs/reference/go-generated#package for more information.
--go_out: protoc-gen-go: Plugin failed with status code 1.
翻译unable to determine Go import path for "student.proto"
后明白大概意思是找不到该文件编译出来的golang文件的导出目录。
我们在.proto
文件中加上如下一行
option go_package="." # 意思是输出到当前目录
具体的proto文件如下
syntax = "proto3";
package helloproto;
option go_package =".";
message Student {
string name = 1;
bool male = 2;
repeated int32 scores = 3;
}
再次执行protoc --go_out=. *.proto
磨了大半天总算成功了。。。
人生在世不容易,十有八九是寄了(bushi
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)