go get国内解决办法汇总
原文鏈接:https://studygolang.com/articles/20364
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/gopmgo 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目錄如下:
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 aftera 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包如下
?
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/grpcD:\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?
?
總結
以上是生活随笔為你收集整理的go get国内解决办法汇总的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《qss样式表笔记大全(二):可设置样式
- 下一篇: golang 使用 redis 的教程