Go 入门教程(2)安装配置 Visual Studio Code

发布 : 2018-03-03 分类 : Notes 浏览 :

我们推荐使用 Visual Studio Code ,它具有自动补全、编码规范(Code Styling)以及许多其他的特性。

下载安装 VS Code

访问官方网站,进行下载并安装。
官方网站
Visual Studio Code

用户设置(User Settings)

点击菜单 文件 > 首选项 > 设置 即可打开用户设置页。也可以通过快捷键 Ctrl+, 来打开用户设置页。我们可以很容易的参考左边的 默认配置 来进行自己定义的设置。
用户设置

调整本地语言

目前vscode 1.21 支持下面10种语言:

  • English (US) en
  • Simplified Chinese 简体中文 zh-CN
  • Traditional Chinese 繁体中文 zh-TW
  • French 法语 fr
  • German 德语 de
  • Italian 意大利语 it
  • Spanish西班牙语 es
  • Japanese 日语 ja
  • Korean 韩语 ko
  • Russian 俄语 ru

通过快捷键 Ctrl+Shift+P 打开 VsCode 的命令面板,输入 config 后就能显示出过滤后的 Configure Language 命令,点击这个命令就会新建或者打开locale.json文件,在这个文件中你就可以设置你的locale 语言了。
调整本地语言
调整本地语言

安装 Go 插件

要开发 Go 程序,需要安装 Go 的插件 Go for Visual Studio Code
IDE Features
它支持以下功能:

  • 自动完成列表 Completion Lists (using gocode)
  • 方法和类的签名帮助信息 Signature Help (using gogetdoc or godef+godoc)
  • Snippets
  • 快速信息 Quick Info (using gogetdoc or godef+godoc)
  • 转到定义 Goto Definition (using gogetdoc or godef+godoc)
  • 查找引用 Find References (using guru)
  • 查找实现 Find implementations (using guru)
  • References CodeLens
  • 文件大纲 File outline (using go-outline)
  • 工作区符号搜索 Workspace symbol search (using go-symbols)
  • 重命名 Rename (using gorename. Note: For Undo after rename to work in Windows you need to - have diff tool in your path)
  • 保存时编译 Build-on-save (using go build and go test)
  • Lint-on-save (using golint or gometalinter)
  • 保存时自动格式化 Format on save as well as format manually (using goreturns or goimports or gofmt)
  • Generate unit tests skeleton (using gotests)
  • 添加导入 Add Imports (using gopkgs)
  • Add/Remove Tags on struct fields (using gomodifytags)
  • Semantic/Syntactic error reporting as you type (using gotype-live)
  • Run Tests under the cursor, in current file, in current package, in the whole - - - workspace (using go test)
  • Show code coverage
  • Generate method stubs for interfaces (using impl)
  • Fill struct literals with default values (using fillstruct)
  • [partially implemented] Debugging (using delve)
  • Upload to the Go Playground (using goplay)

通过快捷键 Ctrl+Shift+X 打开扩展程序面板,找到并安装 Go 插件。
安装 Go 插件

我们在第一次使用 VS Code 编写 Go 程序的时候,编辑器右下角可能会提示需要安装所有依赖的Go工具,此时点击安装所有。
安装go工具
也可以运行 Go: Install/Update tools 命令来安装或更新这些工具。

注意:可能因为网络的原因,部分工具无法直接安装成功。
安装go工具失败

设置代理

打开 文件 → 首选项 → 用户设置
在右侧setting.json里设置以下内容即可。(因为我用的是ss,所以以ss为例)

1
2
"http.proxy": "http://127.0.0.1:1080",
"http.proxyStrictSSL": false

这样设置就大功告成了。重新再安装插件也顺利安装上了。

手动安装

从 VSCode 中的输出日志可以看到有9个工具没有安装成功。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Installing github.com/nsf/gocode SUCCEEDED
Installing github.com/uudashr/gopkgs/cmd/gopkgs SUCCEEDED
Installing github.com/ramya-rao-a/go-outline FAILED
Installing github.com/acroca/go-symbols FAILED
Installing golang.org/x/tools/cmd/guru FAILED
Installing golang.org/x/tools/cmd/gorename FAILED
Installing github.com/fatih/gomodifytags SUCCEEDED
Installing github.com/haya14busa/goplay/cmd/goplay SUCCEEDED
Installing github.com/josharian/impl FAILED
Installing github.com/davidrjenni/reftools/cmd/fillstruct SUCCEEDED
Installing github.com/rogpeppe/godef SUCCEEDED
Installing golang.org/x/tools/cmd/godoc FAILED
Installing github.com/sqs/goreturns FAILED
Installing github.com/golang/lint/golint FAILED
Installing github.com/cweill/gotests/... FAILED
Installing github.com/derekparker/delve/cmd/dlv SUCCEEDED

9 tools failed to install.

我们查看其中某个工具库 go-outline 安装失败的详细日志:

1
2
3
4
5
6
7
8
9
10
go-outline:
Error: Command failed: D:\go\bin\go.exe get -u -v github.com/ramya-rao-a/go-outline
github.com/ramya-rao-a/go-outline (download)
Fetching https://golang.org/x/tools/go/buildutil?go-get=1
https fetch failed: Get https://golang.org/x/tools/go/buildutil?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/tools/go/buildutil: unrecognized import path "golang.org/x/tools/go/buildutil" (https fetch: Get https://golang.org/x/tools/go/buildutil?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.)
github.com/ramya-rao-a/go-outline (download)
Fetching https://golang.org/x/tools/go/buildutil?go-get=1
https fetch failed: Get https://golang.org/x/tools/go/buildutil?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/tools/go/buildutil: unrecognized import path "golang.org/x/tools/go/buildutil" (https fetch: Get https://golang.org/x/tools/go/buildutil?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.)

在工作区目录下 src\github.com\ramya-rao-a\go-outline 可以看见该库是已经下载成功了,只是安装失败,因为安装时用到的依赖文件在 golang.org/x/tools 官网上。解决办法是我们手动下载该工具包到 src 目录中,并安装。

  1. 在工作区目录里的 src 下创建 golang.org/x 目录。
  2. 打开命令行工具,切换到创建的目录下,执行命令 git clone https://github.com/golang/tools.git toolsgit clone https://github.com/golang/lint.git lint 下载两个工具包。
  3. 重启 VSCode ,再次执行安装就OK了。

还有些依然无法安装的,确保对应的库下载成功了,可以进行手动安装。打开命令行工具,切换到工作区的 src 目录,执行类似的命令。

1
go install github.com/ramya-rao-a/go-outline

参考 Go tools that the Go extension depends on

这里必须得吐槽一下,国内的开发人员真的好辛苦,配置个开发工具都费这么半天劲

本文作者 : 王海
原文链接 : https://blog.whai.me/2018/03/03/golang-2-install-vscode/
版权声明 : 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
留下足迹