Go 由於是谷歌研發出品,很多開源依賴包都在國外服務器,down包經常會出現當不下來的情況,此時就需要設置$GOPROXY 代理,通過國內代理鏈接來獲取包資源。
常用代理路徑:https://goproxy.io/
網站打開很直白的介紹瞭如何設置$GOPROXY變量。
In Linux or macOS, you can execute the below commands.
Bash /
# Enable the go modules feature
export GO111MODULE=on
# Set the GOPROXY environment variable
export GOPROXY=https://goproxy.io
Or, write it into the .bashrc or .bash_profile file.
In Windows, you can execute the below commands.
PowerShell
# Enable the go modules feature
$env:GO111MODULE=on
# Set the GOPROXY environment variable
$env:GOPROXY=https://goproxy.io
Now, when you build and run your applications, go will fetch dependencies via goproxy.io. See more information in the goproxy repository.
If your Go version >= 1.13, the GOPRIVATE environment variable controls which modules the go command considers to be private (not available publicly) and should therefore not use the proxy or checksum database. For example:
Go version >= 1.13
go env -w GOPROXY=https://goproxy.io,direct
# Set environment variable allow bypassing the proxy for selected modules
go env -w GOPRIVATE=*.corp.example.com
簡單操作一番
注:首先在通過go get 獲取包時,報invalid $GOPROXY setting error,通過分析報錯信息,並輸出$GOPROXY可見是我的代理鏈接沒對,根據ttps://goproxy.io/中guide執行export後,再go get package方可正常下載資源。最後直接在程序中import包即可使用。
import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
另外一篇關於go 代理設置的博客,推薦給大家。【Go第三方包代理】Go第三方包代理設置 — GOPROXY