vscode的go插件安裝


 

vscode安裝go的很多插件都是失敗,如下:

Installing 5 tools at E:\www\go_project\bin
  go-symbols
  guru
  gorename
  goreturns
  golint

Installing github.com/acroca/go-symbols FAILED
Installing golang.org/x/tools/cmd/guru FAILED
Installing github.com/sqs/goreturns FAILED

1 tools failed to install.

goreturns:
Error: Command failed: D:\softs\Go\bin\go.exe get -u -v github.com/sqs/goreturns
github.com/sqs/goreturns (download)
package golang.org/x/tools/imports: directory "E:\\www\\go_project\\src\\golang.org\\x\\tools\\imports" is not using a known version control system
github.com/sqs/goreturns (download)
package golang.org/x/tools/imports: directory "E:\\www\\go_project\\src\\golang.org\\x\\tools\\imports" is not using a known version control system

Installing golang.org/x/tools/cmd/gorename FAILED
Installing github.com/sqs/goreturns FAILED
Installing golang.org/x/lint/golint FAILED

5 tools failed to install.

go-symbols:
Error: Command failed: D:\softs\Go\bin\go.exe get -u -v github.com/acroca/go-symbols
github.com/acroca/go-symbols (download)
package golang.org/x/tools/go/buildutil: directory "E:\\www\\go_project\\src\\golang.org\\x\\tools\\go\\buildutil" is not using a known version control system
github.com/acroca/go-symbols (download)
package golang.org/x/tools/go/buildutil: directory "E:\\www\\go_project\\src\\golang.org\\x\\tools\\go\\buildutil" is not using a known version control system

guru:
Error: Command failed: D:\softs\Go\bin\go.exe get -u -v golang.org/x/tools/cmd/guru
package golang.org/x/tools/cmd/guru: directory "E:\\www\\go_project\\src\\golang.org\\x\\tools\\cmd\\guru" is not using a known version control system
package golang.org/x/tools/cmd/guru: directory "E:\\www\\go_project\\src\\golang.org\\x\\tools\\cmd\\guru" is not using a known version control system

gorename:
Error: Command failed: D:\softs\Go\bin\go.exe get -u -v golang.org/x/tools/cmd/gorename
package golang.org/x/tools/cmd/gorename: directory "E:\\www\\go_project\\src\\golang.org\\x\\tools\\cmd\\gorename" is not using a known version control system
package golang.org/x/tools/cmd/gorename: directory "E:\\www\\go_project\\src\\golang.org\\x\\tools\\cmd\\gorename" is not using a known version control system

goreturns:
Error: Command failed: D:\softs\Go\bin\go.exe get -u -v github.com/sqs/goreturns
github.com/sqs/goreturns (download)
package golang.org/x/tools/imports: directory "E:\\www\\go_project\\src\\golang.org\\x\\tools\\imports" is not using a known version control system
github.com/sqs/goreturns (download)
package golang.org/x/tools/imports: directory "E:\\www\\go_project\\src\\golang.org\\x\\tools\\imports" is not using a known version control system

golint:
Error: Command failed: D:\softs\Go\bin\go.exe get -u -v golang.org/x/lint/golint
Fetching https://golang.org/x/lint/golint?go-get=1
https fetch failed: Get https://golang.org/x/lint/golint?go-get=1: dial tcp 216.239.37.1:443: connectex: 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.
package golang.org/x/lint/golint: unrecognized import path "golang.org/x/lint/golint" (https fetch: Get https://golang.org/x/lint/golint?go-get=1: dial tcp 216.239.37.1:443: connectex: 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.)
Fetching https://golang.org/x/lint/golint?go-get=1
https fetch failed: Get https://golang.org/x/lint/golint?go-get=1: dial tcp 216.239.37.1:443: connectex: 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.
package golang.org/x/lint/golint: unrecognized import path "golang.org/x/lint/golint" (https fetch: Get https://golang.org/x/lint/golint?go-get=1: dial tcp 216.239.37.1:443: connectex: 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.)

  

最后在網上找到了解決辦法。概要如下:

1.Tips:安裝之前先檢查下 %GoPath%\src\golang.org\x 下的tools/lint是否是從github獲取的最新版本(注意一定要是git clone),正確如下圖所示

因為后面安裝 組件會大量用到這個tools和lint,如果不是最新或不是git clone更新組件會出現大量的:
directory "D:\GoPath\src\golang.org\x\tools\go\buildutil" is not using a known version control system
解決辦法:
由於很多源代碼引用了golang.org官方原生的一些代碼,因此如果不存在的話會導致第三方的源代碼會編譯不通過,第一次使用文件夾不存在創建一個。這與golang本身的包管理機制不健全有很大的關系。
另一篇文章 window下golang包管理glide使用說明也是解決類似問題。go官方也許會在go2.0的版本解決此問題

         1)在%GoPath%\src\golang.org\x 目錄下git clone git@github.com:golang/tools.git,注意,只能git clone

         2)在%GoPath%\src\golang.org\x 目錄下git clone git@github.com:golang/lint.git,注意,只能git clone

2.官方文檔說明 https://github.com/Microsoft/vscode-go/wiki/Go-tools-that-the-Go-extension-depends-on
也可以通過ctrl+shift+p 運行該命令一次性安裝所有這些工具

 

Go: Install/Update Tools

 

組件安裝過程稍微有一點慢,耐心的等待...

安裝的組件 默認安裝狀態 組件備注 github.com->golang.org
go get -u -v github.com/nsf/gocode SUCCEEDED 自動補全  
go get -u -v github.com/uudashr/gopkgs/cmd/gopkgs SUCCEEDED 自動補全未導入的包  
go get -u -v github.com/ramya-rao-a/go-outline SUCCEEDED 當前文件中按符號搜索 https://github.com/golang/tools
go get -u -v github.com/acroca/go-symbols SUCCEEDED 當前workspace中按符號搜索  
go get -u -v golang.org/x/tools/cmd/guru SUCCEEDED 查找所有引用組件  
go get -u -v golang.org/x/tools/cmd/gorename SUCCEEDED 重命名符號  
go get -u -v github.com/fatih/gomodifytags SUCCEEDED 修改結構上的標簽  
go get -u -v github.com/haya14busa/goplay/cmd/goplay SUCCEEDED for running current file in the Go playground  
go get -u -v github.com/josharian/impl SUCCEEDED for generating stubs for interfaces  
go get -u -v github.com/davidrjenni/reftools/cmd/fillstruct SUCCEEDED for filling a struct literal with default values  
go get -u -v github.com/rogpeppe/godef SUCCEEDED 轉到定義2  
go get -u -v golang.org/x/tools/cmd/godoc SUCCEEDED 鼠標懸停顯示文檔注釋2  
go get -u -v sourcegraph.com/sqs/goreturns SUCCEEDED 格式化代碼2  
go get -u -v github.com/golang/lint/golint SUCCEEDED for linting https://github.com/golang/lint
go get -u -v github.com/cweill/gotests/... SUCCEEDED 生成單元測試  
go get -u -v github.com/derekparker/delve/cmd/dlv SUCCEEDED 調試  
go get -u -v github.com/zmb3/gogetdoc   轉到定義2/鼠標懸停顯示注釋2  
go get -u -v golang.org/x/tools/cmd/goimports   格式化代碼2  

經過步驟三的tools和lint的clone后安裝正常都應該是SUCCEEDED ,且不需要牆,如遇個別安裝FAILED 再單獨執行go get ...,看還缺少什么在github.com/golang 中找到相應的組件按同樣的方法clone在golang.org/x文件夾下
TIPS:go版本和組件全部都重新安裝一遍后也能解決vscode 運行go卡的問題,具體原因不詳,建議go版本更新后最好都重新安裝下組件。
之前一直嫌棄vscode運行go卡而用liteide。重新開始用vscode主要原因: 1、顏值高 2、vscode已成為了非常流行的IDE

 

3.cannot find package錯誤

檢查go環境配置,新安裝的go1.10.2的版本默認會在當前用戶新增一個GOPATH導致沖突無法編譯,刪除即可

 

4.調試

       1)vscode打開main.go直接F5就可以運行&調試

       2)生成exe,vscode F5運行會在根路徑下生成一個debug文件,把debug文件重命名加.exe后綴可以直接當應用程序運行
另一種方式cmd切換到項目文件夾下用

go build -o demo.exe main.go

意思是main.go生成名為demo.exe應用程序,go build生成的exe實際比debug要小。

原文地址:https://www.cnblogs.com/nickchou/p/9038114.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM