详解Windows10+golang+gRPC环境搭建


本文摘自php中文网,作者藏色散人,侵删。

下面由golang教程栏目给大家介绍Windows10+golang+gRPC环境搭建,希望对需要的朋友有所帮助!

1、安装protoc

下载地址:https://github.com/protocolbuffers/protobuf/release
(注:https://github.com/protocolbuffers/protobuf 是其源码库,可以学习,如果源码库下载过慢,可以到码云上搜,很多同步的库,是国内的源,下载速度比较快,当然也可以自己在码云上创建个同步的库)

当前最新版本3.12.2
我的是windows10 64位操作系统,所以选择版本:protoc-3.12.2-win64.zip
直接用浏览器即可下载
如果网速不行,还可以用迅雷下载:https://github.com/protocolbuffers/protobuf/releases/download/v3.12.2/protoc-3.12.2-win64.zip
解压之后,将protoc.exe拷贝到$GOPATH/bin目录下
如果有多个GOPATH,放置到放公共第三方库的那个GOPATH中,这样多个project都可以用到

2、安装gRPC

gRPC源码:https://github.com/grpc/grpc-go.git
官网给的安装方法为:go get -u google.golang.org/grpc
但是国内经常会出现如下错误:

1

2

$ go get -u google.golang.org/grpc

package google.golang.org/grpc: unrecognized import path "google.golang.org/grpc" (https fetch: Get https://google.golang.org/grpc?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)

因为google.golang.org在国内很难访问,所以会下载失败。
官网也给了多个解决方案:
https://github.com/grpc/grpc-go
我们采用第二种方法,直接将源码clone到本地
进入到$GOPATH/src目录,执行命令:

1

git clone https://github.com/grpc/grpc-go.git $GOPATH/src/google.golang.org/grpc

下载速度有时快,有时慢,非常慢的时候可以取消,重新触发,多试几次偶尔会很快。
下载完成后,安装gRPC:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

ASUS@LAPTOP-V7SMQSCI MINGW64 ~/go/src

$ go install google.golang.org/grpc/

google.golang.org\grpc\credentials\credentials.go:31:2: cannot find package "github.com/golang/protobuf/proto" in any of:

        D:\Go\src\github.com\golang\protobuf\proto (from $GOROOT)

        C:\Users\ASUS\go\src\github.com\golang\protobuf\proto (from $GOPATH)

google.golang.org\grpc\internal\binarylog\method_logger.go:28:2: cannot find package "github.com/golang/protobuf/ptypes" in any of:

        D:\Go\src\github.com\golang\protobuf\ptypes (from $GOROOT)

        C:\Users\ASUS\go\src\github.com\golang\protobuf\ptypes (from $GOPATH)

google.golang.org\grpc\binarylog\grpc_binarylog_v1\binarylog.pb.go:9:2: cannot find package "github.com/golang/protobuf/ptypes/duration" in any of:

        D:\Go\src\github.com\golang\protobuf\ptypes\duration (from $GOROOT)

        C:\Users\ASUS\go\src\github.com\golang\protobuf\ptypes\duration (from $GOPATH)

google.golang.org\grpc\binarylog\grpc_binarylog_v1\binarylog.pb.go:10:2: cannot find package "github.com/golang/protobuf/ptypes/timestamp" in any of:

        D:\Go\src\github.com\golang\protobuf\ptypes\timestamp (from $GOROOT)

        C:\Users\ASUS\go\src\github.com\golang\protobuf\ptypes\timestamp (from $GOPATH)

google.golang.org\grpc\internal\transport\controlbuf.go:28:2: cannot find package "golang.org/x/net/http2" in any of:

        D:\Go\src\golang.org\x\net\http2 (from $GOROOT)

        C:\Users\ASUS\go\src\golang.org\x\net\http2 (from $GOPATH)

google.golang.org\grpc\internal\transport\controlbuf.go:29:2: cannot find package "golang.org/x/net/http2/hpack" in any of:

        D:\Go\src\golang.org\x\net\http2\hpack (from $GOROOT)

        C:\Users\ASUS\go\src\golang.org\x\net\http2\hpack (from $GOPATH)

google.golang.org\grpc\server.go:36:2: cannot find package "golang.org/x/net/trace" in any of:

        D:\Go\src\golang.org\x\net\trace (from $GOROOT)

        C:\Users\ASUS\go\src\golang.org\x\net\trace (from $GOPATH)

google.golang.org\grpc\status\status.go:34:2: cannot find package "google.golang.org/genproto/googleapis/rpc/status" in any of:

        D:\Go\src\google.golang.org\genproto\googleapis\rpc\status (from $GOROOT)

        C:\Users\ASUS\go\src\google.golang.org\genproto\googleapis\rpc\status (from $GOPATH)

可以发现会有很多错误,根据提示可以发现是由于缺少包的原因,这里就不一点点分析错误信息了,直接给出所需的依赖包以及下载方法(在$GOPATH/src目录下执行命令):
1)text包

1

git clone https://github.com/golang/text.git ./golang.org/x/text

2)net包

1

git clone https://github.com/golang/net.git ./golang.org/x/net

3)genproto包

1

git clone https://github.com/google/go-genproto.git ./google.golang.org/genproto

4)protobuf包
两个:

1

git clone https://github.com/protocolbuffers/protobuf-go.git ./google.golang.org/protobuf

1

git clone https://github.com/golang/protobuf.git ./github.com/golang/protobuf

都要下,github.com/golang/protobuf的代码有的依赖google.golang.org/protobuf

阅读剩余部分

相关阅读 >>

golang中小数点后三位的四舍五入

最长公共子序列

golang基础数据类型-布尔和字符

golang 如何安装包

手撸golang 架构设计原则 接口隔离原则

手撸golang 创建型设计模式 工厂方法

golang的zap日志库的简单封装

golang elasticsearch7的使用

golang基础-高级特性概述

关于golang的make

更多相关阅读请进入《rpc》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...