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