37

go get国内解决办法汇总

 4 years ago
source link: https://studygolang.com/articles/20364?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

go作为Google大佬的亲儿子,go开发过程中除了SDK自带的包外还经常会需要用到大佬提供的其他包,这些包都在golang.org下,而下载这些包需要访问大佬的服务器,国内开发者就会遇到各种墙的问题,今天就给大家总结一下有哪些途径安装这些包.

1. VPN

如果有条件的,公司能够提供稳定可靠的vpn,建议使用VPN,当然也有一些免费的vpn,实测效果很不好

2. gopm

gopm是一个开源的GoPackage Manager的管理工具,在go mod之前比较好使,github地址: https://github.com/gpmgo/gopm

使用思路:

1. 安装gopm

go get -u github.com/gpmgo/gopm

go install gopm

2. 使用gopm安装golang.org包

gopm get golang.org/x/net

实测这个效果在1.11版本后不好使

3. install form github

思路: Google大佬知道很多开发者因为环境限制不能访问golang.org的包,所以这些包在github上都有Copy,但是只是Copy,其中的包依赖还是golang.org,所以目前不能像gopm那样一键全部安装,但是实测可用,接下来以grpc为例,说明如果解决go package 依赖问题.

1. 当前环境

执行go env,主要看两个参数, GOPATH和GOROOT

D:\GoProject>go env

set GOPATH=D:\GoProject

set GOROOT=D:\Go

我的GOPATH目录的src目录如下:

rAfuYv2.png!web

2. 无VPN安装GRPC

grpc的quickstart地址: https://grpc.io/docs/quickstart/go.html

新开一个terminal,执行

go get -u google.golang.org/grpc
D:\GoProject>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: d
dial tcp 216.239.37.1:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of tim dial tcp 216.239.37.1:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of ti
me, or established connection failed because connected host has failed to respond.)
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: connectex: 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.)

很明显,直接提示网络不通

3. 下载grpc-go源码

D:\GoProject\src>git clone https://github.com/grpc/grpc-go ./google.golang.org/grpc
Cloning into './google.golang.org/grpc'...
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 14988 (delta 1), reused 1 (delta 0), pack-reused 14981
Receiving objects: 100% (14988/14988), 7.38 MiB | 344.00 KiB/s, done.
Resolving deltas: 100% (9466/9466), done.

4. go install grpc

D:\GoProject\src>go install google.golang.org/grpc
google.golang.org\grpc\internal\transport\controlbuf.go:27:2: cannot find package "golang.org/x/net/http2" in any of:
        D:\Go\src\golang.org\x\net\http2 (from $GOROOT)
        D:\GoProject\src\golang.org\x\net\http2 (from $GOPATH)
google.golang.org\grpc\internal\transport\controlbuf.go:28:2: cannot find package "golang.org/x/net/http2/hpack" in any of:
        D:\Go\src\golang.org\x\net\http2\hpack (from $GOROOT)
        D:\GoProject\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)
        D:\GoProject\src\golang.org\x\net\trace (from $GOPATH)
google.golang.org\grpc\status\status.go:37: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)
        D:\GoProject\src\google.golang.org\genproto\googleapis\rpc\status (from $GOPATH)

提示缺少golang.org/x/net 和google.golang.org/genproto这两个包

5. 安装golang.org/x/net包和genproto包

在github中找到go组织,搜索到net包如下

M7NJBzU.png!web

D:\GoProject\src>git clone https://github.com/golang/net.git ./golang.org/x/net
Cloning into './golang.org/x/net'...
remote: Enumerating objects: 13, done.
remote: Counting objects: 100% (13/13), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 8291 (delta 5), reused 10 (delta 5), pack-reused 8278
Receiving objects: 100% (8291/8291), 6.47 MiB | 448.00 KiB/s, done.
Resolving deltas: 100% (5758/5758), done.
D:\GoProject\src>git clone https://github.com/google/go-genproto.git ./google.golang.org/genproto
Cloning into './google.golang.org/genproto'...
remote: Enumerating objects: 1474, done.
remote: Counting objects: 100% (1474/1474), done.
remote: Compressing objects: 100% (763/763), done.
remote: Total 11217 (delta 611), reused 1474 (delta 611), pack-reused 9743
Receiving objects: 100% (11217/11217), 15.98 MiB | 451.00 KiB/s, done.
Resolving deltas: 100% (5695/5695), done.
Checking out files: 100% (1268/1268), done.

6. go install grpc

D:\GoProject\src>go install google.golang.org/grpc
golang.org\x\net\idna\idna.go:23:2: cannot find package "golang.org/x/text/secure/bidirule" in any of:
        D:\Go\src\golang.org\x\text\secure\bidirule (from $GOROOT)
        D:\GoProject\src\golang.org\x\text\secure\bidirule (from $GOPATH)
golang.org\x\net\idna\idna.go:24:2: cannot find package "golang.org/x/text/unicode/bidi" in any of:
        D:\Go\src\golang.org\x\text\unicode\bidi (from $GOROOT)
        D:\GoProject\src\golang.org\x\text\unicode\bidi (from $GOPATH)
golang.org\x\net\idna\idna.go:25:2: cannot find package "golang.org/x/text/unicode/norm" in any of:
        D:\Go\src\golang.org\x\text\unicode\norm (from $GOROOT)
        D:\GoProject\src\golang.org\x\text\unicode\norm (from $GOPATH)

我们发现又缺少text包,go on...

D:\GoProject\src>git clone https://github.com/golang/text.git ./golang.org/x/text
Cloning into './golang.org/x/text'...
remote: Enumerating objects: 32, done.
remote: Counting objects: 100% (32/32), done.
remote: Compressing objects: 100% (20/20), done.
remote: Total 5612 (delta 11), reused 32 (delta 11), pack-reused 5580
Receiving objects: 100% (5612/5612), 19.67 MiB | 442.00 KiB/s, done.
Resolving deltas: 100% (3732/3732), done.

D:\GoProject\src>go install google.golang.org/grpc

D:\GoProject\src>

7. 测试安装

基本上go install没有提示错误,grpc就算安装好了,我们现在拿grpc里的example来测试下

// 先启动server
D:\GoProject\src>go run google.golang.org/grpc/examples/helloworld/greeter_server/main.go
2019/03/13 10:50:11 Received: world
//后启动client
D:\GoProject\src>go run google.golang.org/grpc/examples/helloworld/greeter_client/main.go
2019/03/13 10:50:11 Greeting: Hello world

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK