根据Golang 1.14.4版本
插件安装
先声明,本人入门的时候使用的是Liteide,现在虽然在用vscode,但还是感觉Liteide用着更顺。
1.参考本文安装Golang
2.安装VSCode
vscode中按ctrl+shift+x,输入框内搜索Go,选择第一个点击install安装;或者新建一个 .go
文件,用vscode打开会自动提示安装。
3.配置代理
除了go最基础安装没问题,其余扩展插件安装几乎都被墙,因此我们需要用到代理。
访问https://goproxy.io/zh/,在document(文档)菜单下找到windows的配置方式
1. 右键 我的电脑 -> 属性 -> 高级系统设置 -> 环境变量
2. 在 “[你的用户名]的用户变量” 中点击 ”新建“ 按钮
3. 在 “变量名” 输入框并新增 “GOPROXY”
4. 在对应的 “变量值” 输入框中新增 “https://goproxy.io”
5. 最后点击 “确定” 按钮保存设置
如果代理地址不行网上很多代理可以换一下,例如Goproxy 中国(说明)。
配置完成图:
这里的GO111MODULE 是为了用go mod
4. 下载go tools
- 下载安装
打开vscode view
-> Command palette
(命令面板)->搜索 Go:install/update Tools
->点击进入tools全选(亦可ctrl+shift+p
后再选),点击ok,等着安装即可。
如果出现失败就换下代理地址,安装后重启vs
如果正常安装,可能提示如下:
Tools environment: GOPATH=F:\Projects\Golang\System
Installing 0 tool at F:\Projects\Golang\System\bin in module mode.
All tools successfully installed. You are ready to Go. :)
Installing github.com/cweill/gotests/gotests@latest (F:\Projects\Golang\System\bin\gotests.exe) SUCCEEDED
Installing github.com/fatih/gomodifytags@latest (F:\Projects\Golang\System\bin\gomodifytags.exe) SUCCEEDED
Installing github.com/josharian/impl@latest (F:\Projects\Golang\System\bin\impl.exe) SUCCEEDED
Installing github.com/haya14busa/goplay/cmd/goplay@latest (F:\Projects\Golang\System\bin\goplay.exe) SUCCEEDED
Installing github.com/go-delve/delve/cmd/dlv@latest (F:\Projects\Golang\System\bin\dlv.exe) SUCCEEDED
Installing honnef.co/go/tools/cmd/staticcheck@latest (F:\Projects\Golang\System\bin\staticcheck.exe) SUCCEEDED
Installing golang.org/x/tools/gopls@latest (F:\Projects\Golang\System\bin\gopls.exe) SUCCEEDED
All tools successfully installed. You are ready to Go. :)
- 工具配置
如果全选安装后别忘了还需要打开文件->首选项->设置->在 setting.json 中进行设置
"go.goroot": "",
"go.gopath": "",
"go.inferGopath": true,
"go.autocompleteUnimportedPackages": true,
"go.gocodePackageLookupMode": "go",
"go.gotoSymbol.includeImports": true,
"go.useCodeSnippetsOnFunctionSuggest": true,
"go.useCodeSnippetsOnFunctionSuggestWithoutType": false,
"go.docsTool": "gogetdoc",
go mod 一种包的管理方式
modules和传统的GOPATH不同,不需要包含例如src,bin这样的子目录,一个源代码目录甚至是空目录都可以作为module,只要其中包含有go.mod文件。
我们用一个空目录来创建我们的第一个module:
要初始化modules,需要使用如下命令 go mod init 包名
会在对应的目录下生成一个go.mod 文件
接下来编写helloworld
环境变量配置,代理配置,vs安装tools 和插件之后都要重启vscode。。。。