系统环境:

windows10

配置环境:

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

安装(windows)

$ go install github.com/zeromicro/go-zero/tools/goctl@latest
$ goctl -v
goctl version 1.6.0 windows/amd64

4.protoc,protoc-gen-go,protoc-gen-go-grpc安装

方式一:自动安装

如果goctl安装的版本是>=1.3.3版本的,执行以下命令就可以自动安装protoc,protoc-gen-go,protoc-gen-go-grpc三个依赖

# 自动安装
Administrator@wanghaima-PC MINGW64 ~/Desktop
$ 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]: an error interrupted the installation: Get "https://github.com/protocolbuffers/protobuf/releases/download/v3.19.4/protoc-3.19.4-win64.zip": read tcp 192.168.2.140:60882->140.82.114.4:443: wsarecv: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

[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"
"protoc-gen-go" installed from cache
[goctl-env]: "protoc-gen-go" is already installed in "G:\\site\\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"
"protoc-gen-go-grpc" installed from cache
[goctl-env]: "protoc-gen-go-grpc" is already installed in "G:\\site\\go\\bin\\protoc-gen-go-grpc.exe"

[goctl-env]: check env finish, some dependencies is not found in PATH, you can execute
command 'goctl env check --install' to install it, for details, please execute command
'goctl env check --help'

# 安装protoc
Administrator@wanghaima-PC MINGW64 ~/Desktop
$ goctl env check --install 
[goctl-env]: do you want to install "protoc" [y: YES, n: No]
y

# 验证
Administrator@wanghaima-PC MINGW64 ~/Desktop
$ goctl env check --verbose
[goctl-env]: preparing to check env

[goctl-env]: looking up "protoc"
[goctl-env]: "protoc" is installed

[goctl-env]: looking up "protoc-gen-go"
[goctl-env]: "protoc-gen-go" is installed

[goctl-env]: looking up "protoc-gen-go-grpc"
[goctl-env]: "protoc-gen-go-grpc" is installed

[goctl-env]: congratulations! your goctl environment is ready!

方式二: 源文件手动安装:

goctl版本小于 1.3.3 参考以下面源文件安装,单个手动安装protoc,protoc-gen-go,protoc-gen-go-grpc三个依赖

  1. protoc&protoc-gen-go安装

1.1 protoc安装

查看版本

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
作者:海马  创建时间:2023-11-02 23:59
最后编辑:海马  更新时间:2024-05-10 15:32