利用vscode当golang编写IDE的配置过程

vscode以良好的兼容性进来收到追捧。整个vscode执行效率比基于java的ide效率又高。一直用liteide来编写golang,但是liteide调试时断点不能实时增删,每次增删断点,重新运行调试才有用,而且变量窗口观察 变量又不方便。于是乎开始琢磨用vscode。偶然看到一个大神发布的一个vscode包含了golang环境。扩展的名字是:VSCode Essentials

# VSCode Essentials

Extension pack that includes all the tools I use daily as a developer.

## Extensions Included

*   [Angular Language Service](https://marketplace.visualstudio.com/items?itemName=Angular.ng-template "https://marketplace.visualstudio.com/items?itemName=Angular.ng-template")
*   [Angular v6 Snippets by John Papa](https://marketplace.visualstudio.com/items?itemName=johnpapa.Angular2 "https://marketplace.visualstudio.com/items?itemName=johnpapa.Angular2")
*   [angular2-inline by Nate Wallace](https://marketplace.visualstudio.com/items?itemName=natewallace.angular2-inline "https://marketplace.visualstudio.com/items?itemName=natewallace.angular2-inline")
*   [Atom One Light Theme by Mahmoud Ali](https://marketplace.visualstudio.com/items?itemName=akamud.vscode-theme-onelight "https://marketplace.visualstudio.com/items?itemName=akamud.vscode-theme-onelight")
*   [Docker](https://marketplace.visualstudio.com/items?itemName=PeterJausovec.vscode-docker "https://marketplace.visualstudio.com/items?itemName=PeterJausovec.vscode-docker")
*   [EditorConfig](https://marketplace.visualstudio.com/items?itemName=EditorConfig.EditorConfig "https://marketplace.visualstudio.com/items?itemName=EditorConfig.EditorConfig")
*   [ESLint by Dirk Baeumer](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint "https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint")
*   [Go](https://marketplace.visualstudio.com/items?itemName=ms-vscode.Go "https://marketplace.visualstudio.com/items?itemName=ms-vscode.Go")
*   [NativeScript](https://marketplace.visualstudio.com/items?itemName=Telerik.nativescript "https://marketplace.visualstudio.com/items?itemName=Telerik.nativescript")
*   [nginx.conf by shanoor](https://marketplace.visualstudio.com/items?itemName=shanoor.vscode-nginx "https://marketplace.visualstudio.com/items?itemName=shanoor.vscode-nginx")
*   [npm by egamma](https://marketplace.visualstudio.com/items?itemName=eg2.vscode-npm-script "https://marketplace.visualstudio.com/items?itemName=eg2.vscode-npm-script")
*   [One Dark Theme by Joshua Azemoh](https://marketplace.visualstudio.com/items?itemName=azemoh.theme-onedark "https://marketplace.visualstudio.com/items?itemName=azemoh.theme-onedark")
*   [One Monokai Theme by Joshua Azemoh](https://marketplace.visualstudio.com/items?itemName=azemoh.one-monokai "https://marketplace.visualstudio.com/items?itemName=azemoh.one-monokai")
*   [Path Intellisense by Christian Kohler](https://marketplace.visualstudio.com/items?itemName=christian-kohler.path-intellisense "https://marketplace.visualstudio.com/items?itemName=christian-kohler.path-intellisense")
*   [Python](https://marketplace.visualstudio.com/items?itemName=ms-python.python "https://marketplace.visualstudio.com/items?itemName=ms-python.python")
*   [SVG Viewer by cssho](https://marketplace.visualstudio.com/items?itemName=cssho.vscode-svgviewer "https://marketplace.visualstudio.com/items?itemName=cssho.vscode-svgviewer")
*   [TSLint by egamma](https://marketplace.visualstudio.com/items?itemName=eg2.tslint "https://marketplace.visualstudio.com/items?itemName=eg2.tslint")
*   [vscode-elixir by Mat McLoughLin](https://marketplace.visualstudio.com/items?itemName=mjmcloug.vscode-elixir "https://marketplace.visualstudio.com/items?itemName=mjmcloug.vscode-elixir")

立马安装这个扩展,写了golang helloworld. OK,一切正常。 但是,一般golang工程,不可能就一个main.go,vscode如何管理golang工程又不得而知,原来用liteide可以编译调试的工程,放到vscode就不行。后来仔细梳理了一下。终于实现了 vscode全面编译原来的liteide的golang工程。 一下几点要注意: 1,golang工程的目录结构最好要遵守,我的目录结构如下图:

2.png

2,创建一个工作区文件 aaa.code-workspace,如上图,放在根目录,内容如下:

{
    "folders": [
        {
            "path": "H:\\DEVTOOLS\\project\\git\\TntLive\\WebSocketServerGo\\Pic98"
        }
    ],
    "settings": {
        "go.inferGopath": true,
    }
}

3,创建一个launch.json文件(调试菜单,打开配置,就是它),内容如下:

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。src\Pic98\main.go
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${workspaceRoot}/src/Pic98/main.go", // 每次调试启动的文件
            "env": {
                "GOPATH":"C:/Users/Administrator/go;${workspaceRoot}", //可以指定gopath
                "GOROOT":"C:/go",

            },
            "args": []
        }
    ]
}

然后把系统环境变量administraor的path的关于go的目录删除,只留默认的。path变量必须包含goroot/bin;gopath/bin。 最后就是GOPATH/bin(我的是C:\Users\Administrator\go\bin)目录下面,最少要有如下11个文件:

    目录: C:\Users\Administrator\go\bin


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       2018-08-09     11:05       15160320 dlv.exe
-a----       2019-05-30      9:17        4235776 go-outline.exe
-a----       2019-05-30      9:18        4043264 go-symbols.exe
-a----       2019-05-29     20:18       11576832 gocode.exe
-a----       2019-05-30      9:31        9161728 godef.exe
-a----       2018-08-09     14:19        5762048 golint.exe
-a----       2018-08-09     12:05        5111808 gopkgs.exe
-a----       2019-01-18     14:17        9914880 gopm.exe
-a----       2019-05-30      9:30        5974528 gorename.exe
-a----       2018-08-09     13:57        7139840 goreturns.exe
-a----       2019-05-30      9:27        8999424 guru.exe

如果没有就要用go get 安装;比如安装 dlv.exe

go get -u github.com/go-delve/delve/cmd/dlv

大功告成,变量,监视,堆栈,断点,log输出,应有尽有!爽上天了。

vsgolang.png

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券