当前位置 博文首页 > Go 语言 IDE 中的 VSCode 配置使用教程

    Go 语言 IDE 中的 VSCode 配置使用教程

    作者:Go_小易 时间:2021-02-18 06:37

    Gogland 是 JetBrains 公司推出的Go语言集成开发环境。Gogland 同样基于 IntelliJ 平台开发,支持 JetBrains 的插件体系。官方:https://www.jetbrains.com/go/。关于 Goland 相关配置参考该链接即可。Goland 用的好好的,为啥突然想用到 VSCode 呢 ?VSCode 是目前比较流行的 IDE 工具,在功能方面也相对齐全,使用方面也比较友好。不过对于 Golang 来说,配置起来不算太麻烦,只能说其中有一些比较坑的地方,今天我们也就是来填坑的(该死的天朝网络,要不然也不用这么费劲了)。

    一、Golang 环境配置

    关于 Goland 的系统环境配置,这里就不在过多讲解,可以直接参考该链接的前半部分即可。

    二、安装配置 VSCode

    下载链接:https://code.visualstudio.com/

    选择自己系统对应的版本就可以,这个做 IT 的都不知道那就转行吧。

    2.1 安装 golang 插件

    进入Visual Studio Code,使用快捷键F1,打开命令面板

    在上图光标处·输入 exten ,然后选择“Extensions:Install Extension”,如下图

    查询并安装 Go 插件

    提示:在插件列表中,选择 Go,进行安装,安装之后,系统会提示重启Visual Studio Code。由于我这里已经安装过了,所以不会出现 "Install" 选项。

    2.2 开启自动保存功能

    打开Visual Studio Code,找到菜单File -> Preferences -> Settings,如下图:

    选中 "User Settings" 配置如下内容

    {
     "files.autoSave": "onFocusChange",
     "editor.fontSize": 18,
     "go.goroot": "D:\\Go",
     "go.gopath": "D:\\Golang\\MyGO"
    }

    其他保持默认配置即可;

    2.3 执行相关命令(自行安装好git)

    更多详细信息参考:https://marketplace.visualstudio.com/items?itemName=ms-vscode.Go

    1)安装gocode

    打开命令提示符(以管理员身份打开),输入:

    go get -u -v github.com/nsf/gocode

    2)安装godef

    go get -u -v github.com/rogpeppe/godef

    3)安装golint

    # 创建 $GOPATH/src/golang.org/x
    cd $GOPATH/src/golang.org/x
    git clone https://github.com/golang/tools.git
    git clone https://github.com/golang/lint.git
    
    # 完成以上步骤后,执行
    go get golang.org/x/lint/golint

    如果你遇到了类似如下错误,就可以通过 git clone 的方式把包下载到本地,再进行安装即可解决问题;

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

    4)安装go-find-references

    go get -u -v github.com/lukehoban/go-find-references

    5)安装go-outline

    go get -u -v github.com/lukehoban/go-outline

    6)安装goreturns

    go get -u -v sourcegraph.com/sqs/goreturns

    7)安装gorename

    go get -u -v golang.org/x/tools/cmd/gorename

    提示:如果还提示报错,因为前面已经git clone 下来了,可以执行执行"go installgolang.org/x/tools/cmd/gorename";

    8)安装gopkgs

    go get -u -v github.com/tpng/gopkgs

    9)安装go-symbols

    go get -u -v github.com/newhook/go-symbols

    2.4 写一个程序进行测试

    可以看到能够进行相关提示。这里我们执行完整程序测试一下,程序是否正常。

    总结

    js