###### tags: Golang # Golang 配上 Go mod 與 VSCode 環境建置 > MacOS: 10.15.1 > Golang: 1.16.2 ### 準備 1. 至 Golang 官網下載 1.16.2 的 pkg,下載完成後直接點擊下載好的 pkg 檔,安裝路徑可以指定自己想要的位置,Default 會是在 /usr/local/go 下,這個路徑就會是您的 `$GOROOT`。 2. export 相關設定 如果是用 Go Modules 方式開發專案的話,需要執行以下指令 ```shell $ GO111MODULE="on" ``` 當 GO111MODULE 為 on 的時候才會當路徑下有 go.mod 這個檔案的時候會利用 Go Modules 去管理專案,不然都會去 $GOPATH 下抓取需要的套件。 > 如果有遇到 **go.mod exists but should not** 的錯誤訊息,這都是 GOPATH 沒有被設置路徑,GOPATH 會被默認為當下路徑,但 GO111MODULE 又是 on 時這兩者設置就會有衝突,解決方法為將 $GOPATH 設置到指定路徑。 :::danger 如果是 Mac 或 Linux 的話記得要將 export 的資訊設置至 ~/.zshrc 或 ~/.bashrc 下根據您 shell script 的種類 ::: 3. 下載 vscode 並安裝 Go 套件。 我這邊使用 gopls 作為 auto complete 相關的 useLanguageServer ``` "gopls": { "usePlaceholders": true, }, "go.useLanguageServer": true, "go.alternateTools": { "gopkgs": "gopkgs" } ```