系统环境:
windows10 / Linux / macOS
配置环境:
1. golang安装
参考文档:
http://haimait.top/docs/golang/go_install
配置go环境变量:
vim /home/haima/.bashrc
export GOROOT="/usr/local/go" #go源码包
export GOPATH=/home/go # go工作路径
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
source /home/haima/.bashrc
2. go module配置
go env -w GO111MODULE="on"
go env -w GOPROXY=https://goproxy.cn
go env -w GOMODCACHE=$GOPATH/pkg/mod
3. goctl安装
参考:
https://go-zero.dev/docs/tasks/installation/goctl
goctl一键安装
Go 1.16 及以后版本
安装(mac&linux)
GOPROXY=https://goproxy.cn/,direct go install github.com/zeromicro/go-zero/tools/goctl@latest
查看版本
Administrator@wanghaima-PC MINGW64 ~/Desktop
$ goctl -v
goctl version 1.6.0 windows/amd64
# 或者下载批量版本的goctl
$ GO111MODULE=on go install github.com/zeromicro/go-zero/tools/goctl@v1.7.1
$ goctl --version
goctl version 1.7.2 darwin/amd64
安装
## 或者安装最新版本
% go install github.com/zeromicro/go-zero/tools/goctl@latest
% go install github.com/zeromicro/go-zero/tools/goctl@master
## 查看帮助
% goctl -h
## 查看版本
% goctl -v
goctl version 1.7.2 darwin/amd64
4.protoc,protoc-gen-go,protoc-gen-go-grpc安装
方式一:自动安装
如果goctl
安装的版本是>=1.3.3版本的,执行以下命令就可以自动安装protoc,protoc-gen-go,protoc-gen-go-grpc三个依赖
# 自动安装
PS C:\Users\Administrator> goctl env check --install --verbose --force
[goctl-env]: preparing to check env
[goctl-env]: looking up "protoc"
[goctl-env]: "protoc" is not found in PATH
[goctl-env]: preparing to install "protoc"
[goctl-env]: "protoc" is already installed in "D:\\code\\go\\bin\\protoc.exe"
[goctl-env]: looking up "protoc-gen-go"
[goctl-env]: "protoc-gen-go" is not found in PATH
[goctl-env]: preparing to install "protoc-gen-go"
go: downloading google.golang.org/protobuf v1.35.1
[goctl-env]: "protoc-gen-go" is already installed in "D:\\code\\go\\bin\\protoc-gen-go.exe"
[goctl-env]: looking up "protoc-gen-go-grpc"
[goctl-env]: "protoc-gen-go-grpc" is not found in PATH
[goctl-env]: preparing to install "protoc-gen-go-grpc"
go: downloading google.golang.org/grpc v1.67.1
go: downloading google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.5.1
[goctl-env]: "protoc-gen-go-grpc" is already installed in "D:\\code\\go\\bin\\protoc-gen-go-grpc.exe"
[goctl-env]: congratulations! your goctl environment is ready!
验证版本
PS C:\Users\Administrator> protoc --version
libprotoc 3.19.4
PS C:\Users\Administrator> protoc-gen-go --version
protoc-gen-go.exe v1.35.1
PS C:\Users\Administrator> protoc-gen-go-grpc -version
protoc-gen-go-grpc 1.5.1
方式二: 源文件手动安装:
goctl版本小于 1.3.3 参考以下面源文件安装,单个手动安装protoc,protoc-gen-go,protoc-gen-go-grpc三个依赖
- protoc&protoc-gen-go安装
1.1 protoc安装
进入 protobuf release 页面,选择适合自己操作系统的压缩包文件下载,这里我选择
protoc-3.19.4-linux-x86_64.zip
https://github.com/protocolbuffers/protobuf/releases/download/v3.19.4/protoc-3.19.4-linux-x86_64.zip解压出来后进入该目录下的bin目录中,将protoc直接copy到你的gopath/bin目录下即可。
如$GOPATH/bin,这里不建议直接将其和系统的以下path放在一起。export PATH=$PATH:$GOPATH/bin
注意:$GOPATH/bin要加入到系统PATH中,如已加入可跳过
查看版本
haima@haima-PC:~$ protoc --version
libprotoc 3.19.4
1.2 protoc-gen-go 安装
如果goctl 版本已经是1.2.1以后了,可以忽略此步骤。
GOPROXY=https://goproxy.cn/,direct go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
查看$GOPATH/bin下是否有protoc-gen-go即可
【注】:如果后续在使用goctl生成代码时候,遇到以下问题
protoc --proto_path=/Users/seven/Developer/goenv/go-zero-looklook/app/usercenter/cmd/rpc/pb usercenter.proto --go_out=plugins=grpc:/Users/seven/Developer/goenv/go-zero-looklook/app/usercenter/cmd/rpc --go_opt=Musercenter.proto=././pb
goctl: generation error: unsupported plugin protoc-gen-go which installed from the following source:
google.golang.org/protobuf/cmd/protoc-gen-go,
github.com/protocolbuffers/protobuf-go/cmd/protoc-gen-go;
Please replace it by the following command, we recommend to use version before v1.3.5:
go get -u github.com/golang/protobuf/protoc-gen-go
goctl version: 1.3.0 darwin/amd64
直接执行
$ GOPROXY=https://goproxy.cn/,direct go get -u github.com/golang/protobuf/protoc-gen-go
下载包
查看版本
haima@haima-PC:~/Desktop$ protoc-gen-go --version
protoc-gen-go v1.27.1
1.3 protoc-gen-go-grpc 安装
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
查看版本
haima@haima-PC:~$ protoc-gen-go-grpc -version
protoc-gen-go-grpc 1.2.0
以下就是安装好后的四个工具
Administrator@wanghaima-PC MINGW64 /g/site/go/bin
$ tree
.
|-- goctl.exe
|-- protoc-gen-go-grpc.exe
|-- protoc-gen-go.exe
|-- protoc.exe
`-- tree.exe
最后编辑:海马 更新时间:2024-12-22 19:32