創建項目
Location:新項目文件夾GOROOT:go 安裝根目錄Proxy:https://goproxy.io,direct
重要:https://goproxy.io是下載go包的代理地址,設置后可在國內正常下載go倉庫/github的包。
創建一個go文件
創建了一個test.go
下載安裝需要的包
這里演示安裝mysql driver包:github.com/go-sql-driver/mysql
直接import顯示找不到包,使用左下方命令行工具,安裝mysql driver
使用go get 命令安裝包
使用go get github.com/go-sql-driver/mysql安裝mysql driver,安裝成功后test.go中import的包變綠。
我已經安裝過一次,第一次安裝應該會在命令窗口出現一些信息。
補充:Goland 使用 go modules的TIPS
運行go mod tidy,報錯:
go: golang.org/x/sys@v0.0.0-20180905080454-ebe1bf3edb33: unrecognized import path "golang.org/x/sys" (https fetch: Get https://golang.org/x/sys?go-get=1: dial tcp 216.239.37.1:443: c
onnectex: 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.)
go: error loading module requirements
原因是某些依賴在國內被墻了,這邊可以設置一下proxy,就可以解決:
進入Goland的設置界面,File/Settings/Go/Go Module(vgo),設置一下Proxy為:https://goproxy.io。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。如有錯誤或未考慮完全的地方,望不吝賜教。
原文鏈接:https://blog.csdn.net/qq_33398607/article/details/106099939