通常來講國內不能直接訪問golang官方包:因此須要作個代理就是設置GOPROX環境變量golang
設置了Go語言的環境變量:
windows
go env -w GO111MODULE=on go env -w GOPROXY=https://goproxy.cn,direct
設置完後,在GoLand控制檯執行go mod download在執行下載mod報錯:ide
報錯信息是鏈接不上官方網址,但是我明明走了代理不訪問官方網址,走https://goproxy.cn的啊。開始查找緣由。ui
打開cmd,用go env 命令查看:debug
C:\Users\Administrator>go env set GO111MODULE=on set GOARCH=amd64 set GOBIN= set GOCACHE=C:\Users\Administrator\AppData\Local\go-build set GOENV=C:\Users\Administrator\AppData\Roaming\go\env set GOEXE=.exe set GOFLAGS= set GOHOSTARCH=amd64 set GOHOSTOS=windows set GOINSECURE= set GOMODCACHE=C:\Users\Administrator\go\pkg\mod set GONOPROXY=*.corp.example.com set GONOSUMDB=*.corp.example.com set GOOS=windows set GOPATH=C:\Users\Administrator\go set GOPRIVATE=*.corp.example.com set GOPROXY=https://goproxy.cn set GOROOT=D:\Go set GOSUMDB=sum.golang.org set GOTMPDIR= set GOTOOLDIR=D:\Go\pkg\tool\windows_amd64 set GCCGO=gccgo set AR=ar set CC=gcc set CXX=g++ set CGO_ENABLED=1 set GOMOD=NUL set CGO_CFLAGS=-g -O2 set CGO_CPPFLAGS= set CGO_CXXFLAGS=-g -O2 set CGO_FFLAGS=-g -O2 set CGO_LDFLAGS=-g -O2 set PKG_CONFIG=pkg-config set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\ADMINI~1\AppData\Local\Temp\go-build423114610=/tmp/go-build -gno-record-gcc-switches
而後在GoLand控制檯執行go env:代理
C:\Users\Administrator\IdeaProjects\staff-profile>go env set GO111MODULE=on set GOARCH=amd64 set GOBIN= set GOCACHE=C:\Users\Administrator\AppData\Local\go-build set GOENV=C:\Users\Administrator\AppData\Roaming\go\env set GOEXE=.exe set GOFLAGS= set GOHOSTARCH=amd64 set GOHOSTOS=windows set GOINSECURE= set GOMODCACHE=C:\Users\Administrator\IdeaProjects\pkg\mod set GONOPROXY=*.corp.example.com set GONOSUMDB=*.corp.example.com set GOOS=windows set GOPATH=C:\Users\Administrator\IdeaProjects;C:\Users\Administrator\go set GOPRIVATE=*.corp.example.com set GOPROXY=direct set GOROOT=D:\Go set GOSUMDB=sum.golang.org set GOTMPDIR= set GOTOOLDIR=D:\Go\pkg\tool\windows_amd64 set GCCGO=gccgo set AR=ar set CC=gcc set CXX=g++ set CGO_ENABLED=1 set GOMOD=C:\Users\Administrator\IdeaProjects\staff-profile\go.mod set CGO_CFLAGS=-g -O2 set CGO_CPPFLAGS= set CGO_CXXFLAGS=-g -O2 set CGO_FFLAGS=-g -O2 set CGO_LDFLAGS=-g -O2 set PKG_CONFIG=pkg-config set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\ADMINI~1\AppData\Local\Temp\go-build682176362=/tmp/go-build -gno-record-gcc-switches
兩個GOPROXY的值不同,在GoLand控制檯的GOPROXY的值是direct,仍是走官方默認的網址去下載依賴包。。code
因而從新設置代理地址,當我在GoLand控制檯設置代理時GoLand報錯:blog
warning: go env -w GOPROXY=... does not override conflicting OS environment variablecmd
後來找了好久緣由,這是由於在環境變量中有這個值,在電腦的環境變量果真找到了這個以前裝Go環境設置的值,把它刪除,保存,重啓GoLand後,在下面的界面從新設置:it
而後在GoLand控制檯設置:
go env -w GOPROXY=https://goproxy.cn,direct
而後就能夠執行go mod download了。