windows golang安装Grpc

一、 下载并配置protoc.exe

在github下载链接:https://github.com/protocolbuffers/protobuf/releases 选择最新的 protoc-3.19.0-win64.zip,解压缩,找到bin目录下的protoc.exe,将protoc.exe放到GOPATH/bin目录下,我的GOPATH的目录是F:go_learning,所以我把protoc.exe 放在了F:go_learningin下,为了确保能找到protoc.exe,需要在系统环境变量的PATH设置F:go_learningin

 

 使用命令行工具,进入F:go_learningin,输入protoc.exe,如下图发现就能找到了

 可以使用命令查看版本

protoc --version

以上步骤切记不可少,不然就会出现:'protoc' 不是内部或外部命令,也不是可运行的程序 或批处理文件。

二、安装protobuf和gRPC

使用命令安装protobuf

go get -u github.com/golang/protobuf/proto
go get -u github.com/golang/protobuf/protoc-gen-go

安装完后,会在GOPATH/bin目录下生成protoc-gen-go.exe

使用命令安装gRPC

正常在国内网络通过下面的命令是安装失败,如果可以就只需要执行下面的语句

go get -u google.golang.org/grpc

如果不行,使用以下几条命令代替

# 下载net包
git clone https://github.com/golang/net.git $GOPATH/src/golang.org/x/net
# 下载text包
git clone https://github.com/golang/text.git $GOPATH/src/golang.org/x/text

#下载go-genproto包
git clone https://github.com/google/go-genproto.git $GOPATH/src/google.golang.org/genproto
# 下载grpc-go包
git clone https://github.com/grpc/grpc-go.git $GOPATH/src/google.golang.org/grpc

切换到GOPATH/src目录,执行如下命令:

go install google.golang.org/grpc

三、验证是否安装成功

切换到F:go_learningsrcgoogle.golang.orggrpcexampleshelloworldhelloworld目录下,使用demo的helloword.proto 测试 

protoc --go_out=plugins=grpc:. helloworld.proto
# 运行成功后会生成helloworld.pb.go文件
原文地址:https://www.cnblogs.com/cxt618/p/15467428.html