最近在尝试用go-zero搭建商城系统。
过程涉及用proto文件生成rpc的go代码,报错如下:
"protoc-gen-go" installed from cache
"protoc-gen-go-grpc" installed from cache
Error: 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.
Usage:
goctl rpc protoc [flags]
Examples:
goctl rpc protoc xx.proto --go_out=./pb --go-grpc_out=./pb --zrpc_out=.
Flags:
--branch string The branch of the remote repo, it does work with --remote
-h, --help help for protoc
--home string The goctl home path of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
-m, --multiple Generated in multiple rpc service mode
--remote string The remote git repo of the template, --home and --remote cannot be set at the same time, if they are, --remote has higher priority
The git repo directory must be consistent with the https://github.com/zeromicro/go-zero-template directory structure
--style string The file naming format, see [https://github.com/zeromicro/go-zero/tree/master/tools/goctl/config/readme.md] (default "gozero")
-v, --verbose Enable log output
--zrpc_out string The zrpc output directory
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.
解决方法如下:
1.先查看是否有安装protoc-gen-go,没有就安装下(下面是mac的命令)
ayakadeMacBook-Pro:Cellar ayaka$ pwd
/usr/local/Cellar
ayakadeMacBook-Pro:Cellar ayaka$ ls
goctl protobuf
ayakadeMacBook-Pro:Cellar ayaka$ brew install protoc-gen-go
==> Downloading https://ghcr.io/v2/homebrew/core/protoc-gen-go/manifests/1.28.1
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/protoc-gen-go/blobs/sha256:73b3
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sh
######################################################################## 100.0%
==> Pouring protoc-gen-go--1.28.1.catalina.bottle.tar.gz
🍺 /usr/local/Cellar/protoc-gen-go/1.28.1: 5 files, 6MB
==> Running `brew cleanup protoc-gen-go`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
ayakadeMacBook-Pro:Cellar ayaka$ ls
goctl protobuf protoc-gen-go
2.再将添加环境变量到.bash_profile
vim ~/.bash_profile
文件末尾添加:
export GO_PATH=~/go
export PATH=$PATH:/$GO_PATH/bin
source ~/.bash_profile
顺便普及几个概念:
1.Protobuf(Protocol Buffer)是google 的一种数据交换的格式,它独立于语言,独立于平台
2.protoc是protobuf文件(.proto)的编译器(参考链接),可以借助这个工具把 .proto 文件转译成各种编程语言对应的源码,包含数据类型定义、调用接口等;
protoc的编译过程分成简单的两个步骤(如上图所示):1)解析.proto文件,转译成protobuf的原生数据结构在内存中保存;2)把protobuf相关的数据结构传递给相应语言的编译插件,由插件负责根据接收到的protobuf原生结构渲染输出特定语言的模板
3.protoc-gen-go是protobuf编译插件系列中的Go版本