首頁 > 軟體

VS Code安裝go外掛失敗原因分析以及解決方案

2022-09-08 18:04:31

問題背景

VSCode是我們開發go程式的常用工具,但是安裝VSCode成功後,建立一個.go檔案會有如下提示:

這個是vscode提示你需要安裝go外掛,但是當你點選install all進行安裝時,發現會安裝失敗。

Installing 8 tools at D:pragramsgobin
  gocode
  gopkgs
  go-outline
  go-symbols
  dlv
  godef
  goreturns
  golint
 
Installing github.com/mdempsky/gocode FAILED
Installing github.com/uudashr/gopkgs/cmd/gopkgs FAILED
Installing github.com/ramya-rao-a/go-outline FAILED
Installing github.com/acroca/go-symbols FAILED
Installing github.com/derekparker/delve/cmd/dlv FAILED
Installing github.com/rogpeppe/godef FAILED
Installing github.com/sqs/goreturns FAILED
Installing golang.org/x/lint/golint FAILED
 
8 tools failed to install.
 
gocode:
Error: Command failed: D:installgobingo.exe get -u -v github.com/mdempsky/gocode
go: missing Git command. See https://golang.org/s/gogetcmd
package github.com/mdempsky/gocode: exec: "git": executable file not found in %PATH%
go: missing Git command. See https://golang.org/s/gogetcmd
package github.com/mdempsky/gocode: exec: "git": executable file not found in %PATH%
 
gopkgs:
Error: Command failed: D:installgobingo.exe get -u -v github.com/uudashr/gopkgs/cmd/gopkgs
go: missing Git command. See https://golang.org/s/gogetcmd
package github.com/uudashr/gopkgs/cmd/gopkgs: exec: "git": executable file not found in %PATH%
go: missing Git command. See https://golang.org/s/gogetcmd
package github.com/uudashr/gopkgs/cmd/gopkgs: exec: "git": executable file not found in %PATH%
 
go-outline:
Error: Command failed: D:installgobingo.exe get -u -v github.com/ramya-rao-a/go-outline
go: missing Git command. See https://golang.org/s/gogetcmd
package github.com/ramya-rao-a/go-outline: exec: "git": executable file not found in %PATH%
go: missing Git command. See https://golang.org/s/gogetcmd
package github.com/ramya-rao-a/go-outline: exec: "git": executable file not found in %PATH%
 
go-symbols:
Error: Command failed: D:installgobingo.exe get -u -v github.com/acroca/go-symbols
go: missing Git command. See https://golang.org/s/gogetcmd
package github.com/acroca/go-symbols: exec: "git": executable file not found in %PATH%
go: missing Git command. See https://golang.org/s/gogetcmd
package github.com/acroca/go-symbols: exec: "git": executable file not found in %PATH%
 
dlv:
Error: Command failed: D:installgobingo.exe get -u -v github.com/derekparker/delve/cmd/dlv
go: missing Git command. See https://golang.org/s/gogetcmd
package github.com/derekparker/delve/cmd/dlv: exec: "git": executable file not found in %PATH%
go: missing Git command. See https://golang.org/s/gogetcmd
package github.com/derekparker/delve/cmd/dlv: exec: "git": executable file not found in %PATH%
 
godef:
Error: Command failed: D:installgobingo.exe get -u -v github.com/rogpeppe/godef
go: missing Git command. See https://golang.org/s/gogetcmd
package github.com/rogpeppe/godef: exec: "git": executable file not found in %PATH%
go: missing Git command. See https://golang.org/s/gogetcmd
package github.com/rogpeppe/godef: exec: "git": executable file not found in %PATH%
 
goreturns:
Error: Command failed: D:installgobingo.exe get -u -v github.com/sqs/goreturns
go: missing Git command. See https://golang.org/s/gogetcmd
package github.com/sqs/goreturns: exec: "git": executable file not found in %PATH%
go: missing Git command. See https://golang.org/s/gogetcmd
package github.com/sqs/goreturns: exec: "git": executable file not found in %PATH%
 
golint:
Error: Command failed: D:installgobingo.exe get -u -v golang.org/x/lint/golint
go: missing Git command. See https://golang.org/s/gogetcmd
package golang.org/x/lint/golint: exec: "git": executable file not found in %PATH%
go: missing Git command. See https://golang.org/s/gogetcmd
package golang.org/x/lint/golint: exec: "git": executable file not found in %PATH%

問題原因

 在安裝go外掛時,會自動更新很多依賴庫檔案,都是從Github更新下來,但是因為Github的檔案中,多有應用go官網中的檔案,因為一些網路國內無法存取,網路緣故,不能直接下載,導致安裝失敗。

解決方案

方案1:快速方案

核心是設定國內下載源,我們需要修改如下兩個go的環境設定:

go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn

設定好兩個變數後,重新開啟VSCode,點選右下方的install all重新安裝,

或者,在vscode中使用Ctrl+Shift+P,輸入>go:install,下面會自動搜尋相關命令,我們選擇Go:Install/Update Tools這個命令,選中所有外掛,點選確定進行安裝。

問題成功解決:

方案2:環境變數中設定

Windows在電腦 -> 系統 -> 高階系統設定 -> 使用者環境中分別新建GO111MODULE和GOPROXY兩個使用者變數,其值如下圖所示:

GO111MODULE=on
GOPROXY=https://goproxy.cn,direct

或者也可以使用阿里源代理如下:

GO111MODULE=on
GOPROXY=https://mirrors.aliyun.com/goproxy/   
 

設定好之後,使用Windows + R調出終端,輸入cmd,通過go env命令檢視go的環境變數設定是否設定成功。 

方案3:vscode中設定

vscode編輯器的設定在:檔案 -> 偏好設定 -> 設定 -> 使用者 -> 應用程式 -> 代理伺服器路徑下,如下圖所示:

總結

到此這篇關於VS Code安裝go外掛失敗原因分析以及解決方案的文章就介紹到這了,更多相關VS Code安裝go外掛失敗解決內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com