Go开发工具

本篇介绍主要介绍VSCODE,其他开发工具可参考Go开发工具

Visual Studio Code(VSCODE)

vscode是微软基于Electron和web技术构建的开源编辑器,是一款很强大的编辑器,开源地址:https://github.com/Microsoft/vscode

1. 安装Vscode最新版

官网:https://code.visualstudio.com/ 下载最新版;

2. 安装Go插件

点击右边的 Extensions 图标 搜索Go插件 在插件列表中,选择 Go,进行安装,安装之后,系统会提示重启 Visual Studio Code;

建议把自动保存功能开启。开启方法为:选择菜单 File,点击 Auto save;

vscode代码设置可用于Go扩展。这些都可以在用户的喜好来设置或工作区设置(.vscode/settings.json);

打开首选项-用户设置 settings.json:

  1. {
  2. "go.buildOnSave": true,
  3. "go.lintOnSave": true,
  4. "go.vetOnSave": true,
  5. "go.buildFlags": [],
  6. "go.lintFlags": [],
  7. "go.vetFlags": [],
  8. "go.coverOnSave": false,
  9. "go.useCodeSnippetsOnFunctionSuggest": false,
  10. "go.formatOnSave": true,
  11. //goimports
  12. "go.formatTool": "goreturns",
  13. "go.goroot": "", //Your_Goroot
  14. "go.gopath": "", //Your_Gopath
  15. }

接着安装依赖包支持(网络不稳定,请直接到 GitHub Golang 下载再移动到相关目录):

  1. go get -u -v github.com/nsf/gocode
  2. go get -u -v github.com/rogpeppe/godef
  3. go get -u -v github.com/zmb3/gogetdoc
  4. go get -u -v github.com/golang/lint/golint
  5. go get -u -v github.com/lukehoban/go-outline
  6. go get -u -v sourcegraph.com/sqs/goreturns
  7. go get -u -v golang.org/x/tools/cmd/gorename
  8. go get -u -v github.com/tpng/gopkgs
  9. go get -u -v github.com/newhook/go-symbols
  10. go get -u -v golang.org/x/tools/cmd/guru
  11. go get -u -v github.com/cweill/gotests/...

vscode还有一项很强大的功能就是断点调试,结合delve可以很好的进行Go代码调试
```
go get -v -u github.com/peterh/liner github.com/derekparker/delve/cmd/dlv

  1. brew install go-delve/delve/delve(mac可选)

注意:修改”dlv-cert”证书,选择”显示简介”->”信任”->”代码签名”修改为:始终信任;

打开首选项-工作区设置,配置launch.json:

  1. {
  2. "version": "0.2.0",
  3. "configurations": [
  4. {
  5. "name": "main.go",
  6. "type": "go",
  7. "request": "launch",
  8. "mode": "debug",
  9. "remotePath": "",
  10. "port": 2345,
  11. "host": "127.0.0.1",
  12. "program": "${workspaceRoot}", //工作空间路径
  13. "env": {},
  14. "args": [],
  15. "showLog": true
  16. }
  17. ]
  18. }

```