使用 VS Code 快速搭建 Golang 开发环境

版权声明:本文为aggresss原创文章,未经博主允许不得转载。 作者:[email protected] https://blog.csdn.net/aggresss/article/details/79700740

  刚开始接触 Golang 时使用的是 Eclipse + Goclipse 开发环境,但是你懂的,Eclipse的体量有点大,当有一个非常好的 idea 时 Launch and Create the project 的时间足以让灵感飞走了,寻找一款轻量级又耐用的 golang 款 IDE 一直潜伏在心里。第一次接触到 VS Code 时是一个演讲视频使用 VS code 进行 Python 快速开发,于是下载下来,发现竟然还有 Go 这个插件,从此便爱不释手。这货和 Visual Studio 虽然出自同门,但走的和他哥完完全全是两条路,这家伙是一个狂野的,细腻的,快速迭代的,社区化运营的开放性IDE。
  安装和启动 VS Code 可以根据自己的操作系统平台分别下载,成功的安装 Go 插件需要一个科学的网络环境,具体配置可以参考 GoGetPorxyConfig . Linux 和 MacOS 下 Shell 的代理设置比较容易,如果在 Windows 环境下可以参考一下这个 Repo 里的 proxy-config 文件,通过 PowerShell 实现 Shell 的代理切换。
  安装 Golang 运行时 是必须的,具体的安装和设置可以参考 官方文档,其中 How to Write Go Code 可以详细阅读以下,毕竟Golang作为一种完全开源的语言,代码目录的组织方式可能会和传统语言有点区别。GOROOT 和 GOPATH 的环境变量需要设置,同时需要将 GOROOT/bin 和 GOPATH/bin 加入到 PATH 变量中,并且我们的开发目录将来也会包含在 GOPATH 中,和其他第三方应用放在一起,代码的索引方式是通过代码的远程 Repo 地址来工作的,也就是说想要开发 Golang 程序,你需要先有一个程序使用者可以访问到的 Repo 才行。
  先大体了解以下 VS Code,总结了一下一个 IDE 的基本功能大概包括:

  • Linting,
  • Debugging (multi-threaded, remote),
  • Intellisense,
  • Code Formatting,
  • Refactoring,
  • Unit Tests,
  • Snippets, and more.

  一般的IDE都是定义框架然后通过插件的形式支持各种语言, VS Code 也不例外,每种语言也都只是提供了一个基本运行时(Runtime),IDE的功能也需要第三方去实现,比如 Pyhon 中 代码检查可以选择 Pylint 或者 Flake8,代码格式化可以选择 Autopep8 或者 Yapf . 同样,Golang 虽然是开箱即用的语言,但是想要写出规范的代码还是需要很多第三方的代码工具来协助的。下载安装并运行 VS Code 后,使用 Ctrl + Shift + x 快捷键 进入插件管理窗口 输入 “Go” ,搜索结果中 作者为 lukehoban 的插件就是比较推荐的 Go 插件, 安装后即可使用,插件的 repo https://github.com/Microsoft/vscode-go
Go 插件列出了实现代码编辑功能用到的第三方应用:

  • 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)

具体包括:

// Tools used explicitly by the basic features of the extension
const importantTools = [
    'gocode',
    'gopkgs',
    'go-outline',
    'go-symbols',
    'guru',
    'gorename',
    'godef',
    'godoc',
    'gogetdoc',
    'goreturns',
    'goimports',
    'golint',
    'gometalinter',
    'megacheck',
    'dlv'
];

检索它所用第三方插件的下载地址:
https://github.com/Microsoft/vscode-go/blob/master/src/goInstallTools.ts
对于强迫症患者,可以使用下面的命令一次性把第三方应用全部安装(一共22个应用,前15个为 Tools used explicitly by the basic features of the extension ):

go get -u -v github.com/nsf/gocode
go get -u -v github.com/uudashr/gopkgs/cmd/gopkgs
go get -u -v github.com/ramya-rao-a/go-outline
go get -u -v github.com/acroca/go-symbols
go get -u -v golang.org/x/tools/cmd/guru
go get -u -v golang.org/x/tools/cmd/gorename
go get -u -v github.com/rogpeppe/godef
go get -u -v golang.org/x/tools/cmd/godoc
go get -u -v github.com/zmb3/gogetdoc
go get -u -v github.com/sqs/goreturns
go get -u -v golang.org/x/tools/cmd/goimports
go get -u -v github.com/golang/lint/golint
go get -u -v github.com/alecthomas/gometalinter
go get -u -v honnef.co/go/tools/...
go get -u -v github.com/derekparker/delve/cmd/dlv

go get -u -v github.com/haya14busa/goplay/cmd/goplay
go get -u -v github.com/josharian/impl
go get -u -v github.com/tylerb/gotype-live
go get -u -v github.com/cweill/gotests/...
go get -u -v github.com/sourcegraph/go-langserver
go get -u -v github.com/davidrjenni/reftools/cmd/fillstruct
go get -u -v github.com/fatih/gomodifytags

再安装go vendor 机制 依赖管理工具 glide , Godepgovendor :

curl https://glide.sh/get | sh
go get -u -v github.com/golang/dep/cmd/dep
go get -u github.com/kardianos/govendor

这三种 vendor 管理工具对应的配置文件分别为:

  1. glide -> glide.yml
  2. Godep -> Gopkg.toml
  3. govendor -> vendor.json

为了方便将当前工作目录加入到 $GOPATH 中,将下面指令加入到 ~/.bashrc 文件中:

# set $PWD append to $GOPATH
gopath-pwd(){
  if [[ $GOPATH =~ .*$PWD.* ]];then
    echo "currnet dir is already in GOPATH"
  else
    export GOPATH=$GOPATH:$PWD
    echo "fininsh setting $PWD in GOPATH"
  fi
}
export -f gopath-pwd

如果是 MacOS 系统,为了方便命令行启动 VS Code 需要创建一个软连接:

ln -s /Applications/Visual\ Studio\ Code.app/Contents/Resources/app/bin/code ~/bin/code

如果工程目录不在 $GOPATH 中,需要在 VS Code 中使用
Ctrl+Shift+P -> Open workspace settings 中添加一下信息

"go.inferGopath": true,

保存配置后,VS Code 会将当前目录附加到 $GOPATH 中。

安装完成后,可以配置 launch.json (which is where VS Code stores the debugger configurations)
文件,使用 Ctrl+Shift+D 进入调试视图,选择一个调试配置,然后 F5 快捷键 调试 Golang 程序,可以使用 Ctrl + p 中输入 “debug add config” 根据向导来添加使用于 Golang 的 Configuration .
debug add config

  描述一下 VS Code 的 workspace 的概念。VS Code 定义于一个轻量级的 IDE ,连 workspace 也是轻量级的,它的 workspace 就是一个 json 配置文件,里面记录了这个 workspace 包含的 folders 和 settings

{
    "folders": [
        {
            "path": "xxxxxx"
        },
        {
            "path": "yyyyyy"
        }
    ],
    "settings": {

    }
}

只要保存一个 workspace 文件就可以同时打开多个目录,非常方便。同时 VS Code 的组织层次为:
User -> Workspace -> Floder 三个级别,可以根据自己的实际需求灵活配置。

  最后再描述一下 VS Code 的 tasks 的概念。有很多工具可以自动执行任务,如 linting, building, packaging, testing or deploying software systems,这些工具大部分都是从命令行运行的,并在内部软件开发循环(编辑,编译,测试和调试)内外自动执行作业。鉴于它们在开发生命周期中的重要性,能够在VS Code中运行工具并分析其结果非常有帮助。因此在目录下创建 tasks.json 文件,自定义任务可以有效的提高开发效率。

下面自定义了一个 编译当前打开文件的 tasks:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "go-build-current",
            "type": "shell",
            "command": "go",
            "args": [
                "build",
                "${relativeFile}",
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
    ]
}

参考文档:

https://github.com/golang/go/wiki

猜你喜欢

转载自blog.csdn.net/aggresss/article/details/79700740