VS Code Configuration Go language development environment

VS Code is a Microsoft open source editor, very rich plugin system. This article describes how to use VS Code to build the Go language development environment.

VS Code Configuration Go language development environment

Said in front of the words, Go language is the use UTF8 encoding, in theory, use any text editor can do Go language development. You can choose according to their preferences. Editor / IDE is no best only the most suitable.

Download and install

VS CodeOfficial Download: https://code.visualstudio.com/Download

Three major platforms are supported, according to their own computer platform to select the corresponding installation package. 1550239338474.pngDouble-click the downloaded installation file, double-click to install.

Simplified Chinese is installed plug-ins

Click on the left menu bar last item 管理扩展in 搜索框the input chinese, select the result list of the first item, click installinstall.

After installation will prompt the bottom right corner 重启VS Code, and then restart your VS Code to display Chinese friends! vscode1.gif VSCodeThe main interface description:1550240342443.png

Go install extensions development

Now we want to install our VS Code Editor Goextension, it supports the Go language development.1550241460281.png

Change Theme Editor

Click 首选项->颜色主题will pop up the following window: 15535744481737.jpgSuggest a similar theme Sublime Text style Monokai, bright style and theme Light(Visual Studio). The next demo is in Light(Visual Studio)screenshots theme.

Installation Go Language Development Kit

Go here to provide language development when such code hints for us, code completion feature congruent.

Press the Windows platform Ctrl+Shift+P, Mac platform by Command+Shift+Pthis time VS Code input screen will pop up a box, as shown below:15535662106193.jpg

We are in the input box >go:install, the following will automatically search for related commands, we choose Go:Install/Update Toolsthis command15535659707162.jpg

选中并会回车执行该命令(或者使用鼠标点击该命令) 15535665573387.jpg

然后点击“确定”按钮,进行安装。 会弹出如下输入窗口: 15535666751393.jpg

VS Code此时会下载并安装上图列出来的16个工具,但是由于国内的网络环境基本上都会出现安装失败,如下图各种FAILED: 15535675759821.jpg

有两种方法解决这个问题:

方法一:使用git下载源代码再安装

我们可以手动从github上下载工具,(执行此步骤前提需要你的电脑上已经安装了git)

第一步:现在自己的GOPATHsrc目录下创建golang.org/x目录

第二步:在终端/cmdcdGOPATH/src/golang.org/x目录下

第三步:执行git clone https://github.com/golang/tools.git tools命令

第四步:执行git clone https://github.com/golang/lint.git命令

第五步:按下Ctrl/Command+Shift+P再次执行Go:Install/Update Tools命令,在弹出的窗口全选并点击确定,这一次的安装都会SUCCESSED了。

经过上面的步骤就可以安装成功了。 这个时候创建一个Go文件,就能正常使用代码提示、代码格式化等工具了。

方法二:下载已经编译好的可执行文件

如果上面的步骤执行失败了或者懒得一步一步执行,可以直接下载我已经编译好的可执行文件,拷贝到自己电脑上的 GOROOT/bin 目录下。 go-tools百度云下载链接,密码:vjx2。

注意:特别是Mac下需要给拷贝的这些文件赋予可执行的权限。

配置自动保存

依次点击 首选项->设置,打开设置页面就能看到自动保存相关配置如图,可以根据自己的喜好选择自动保存的方式: 15535683208695.jpg

配置代码片段快捷键

还是按Ctrl/Command+Shift+P,按下图输入>snippets,选择命令并执行: 15535687503862.jpg

Then click on the pop-up window select gothe option: 15535688890224.jpgthen pop up the following page:15535689514491.jpg

We can simply look at the note above, describes the main usage:

“这里放个名字”:{
"prefix": "这个是快捷键",
"body": "这里是按快捷键插入的代码片段",
"description": "这里放提示信息的描述"
}


<p>其中<code>$0</code>表示最终光标提留的位置。
举个例子,我这里创建了两个快捷方式,一个是输入<code>pln</code>就会在编辑器中插入<code>fmt.Println()</code>代码;输入<code>plf</code>,就会插入<code>fmt.Printf(&quot;&quot;)</code>代码。</p>

<pre><code class="language-json">{
    &quot;println&quot;:{
        &quot;prefix&quot;: &quot;pln&quot;,
        &quot;body&quot;:&quot;fmt.Println($0)&quot;,
        &quot;description&quot;: &quot;println&quot;
    },
    &quot;printf&quot;:{
        &quot;prefix&quot;: &quot;plf&quot;,
        &quot;body&quot;: &quot;fmt.Printf(\&quot;$0\&quot;)&quot;,
        &quot;description&quot;: &quot;printf&quot;
    }
}

添加如上配置后,保存。 我们打开一个go文件,测试一下效果:demo1.gif


未完待续…

Guess you like

Origin www.cnblogs.com/nickchen121/p/11517473.html