go get国内解决办法汇总

栏目: Go · 发布时间: 5年前

内容简介:go作为Google大佬的亲儿子,go开发过程中除了SDK自带的包外还经常会需要用到大佬提供的其他包,这些包都在golang.org下,而下载这些包需要访问大佬的服务器,国内开发者就会遇到各种墙的问题,今天就给大家总结一下有哪些途径安装这些包.如果有条件的,公司能够提供稳定可靠的vpn,建议使用VPN,当然也有一些免费的vpn,实测效果很不好gopm是一个开源的GoPackage Manager的管理工具,在go mod之前比较好使,github地址:

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目录如下:

go get国内解决办法汇总

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包如下

go get国内解决办法汇总

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

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

计算机程序设计艺术(第2卷)

计算机程序设计艺术(第2卷)

Donald E. Knuth / 苏运霖 / 国防工业出版社 / 2002-8 / 98.00元

本书是国内外业界广泛关注的7卷本《计算机程序设计艺术》第2卷的最新版。本卷对半数值算法领域做了全面介绍,分“随机数”和“算术”两章。本卷总结了主要算法范例及这些算法的基本理论,广泛剖析了计算机程序设计与数值分析间的相互联系,其中特别值得注意的是作者对随机数生成程序的重新处理和对形式幂级数计算的讨论。 本书附有大量习题和答案,标明了难易程度及数学概念的使用。 本书内容精辟,语言流畅,引人入胜,可供从......一起来看看 《计算机程序设计艺术(第2卷)》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试