Vscode Golang Environment Configuration

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Vscode Golang Environment Configuration

    • Get ready

After installing the Vscode, install the Golang plugin, you can use the Ctrl+shift+p or F1 call out command panel, enter extensions, enter the keyword go and then install

Then you pull a folder into Vscode, and then you do the workspace settings task through the preferences----and workspace settings. In the profile Settings.json of the workspace, we need to set the Go.gopath

{    "go.gopath": "${workspaceRoot}/../../../../",    "go.buildOnSave": false}

After setting up the Gopath, our workspace settings are complete.

    • Tools to rely on

The Go plugin relies on many tools to complete code hinting, lint, and more. There are two options to download in the Vscode prompt, or to download the compiled binary package directly. In Git I have compiled a variety of dependency packages, it should be noted that only 64-bit environment in order to support delve debugging support, we unpack the compressed package, put into the Gopath/bin directory, so the dependent tool is ready.

    • Shortcut key build and install

To quickly implement build and install, we use tasks here to accomplish this task. First, we call out the command panel, enter Tasks:configure task Runner, and then we enter the Tasks.json edit window, and we configure a build task in the following way, configured under the Tasks array:

{        "taskName": "vscgo_build",        "isBuildCommand": true,        "args": [            "${workspaceRoot}",            "${fileDirname}"        ],        "isWatching": false,        "problemMatcher": {            "owner": "go",            "fileLocation": [                "relative",                "${fileDirname}"            ],            "pattern": {                "regexp": "^(.*):(\\d+):\\s+(.*)$",                "file":1,                "line": 2,                "message": 3        }    }}

By setting Isbuildcommand, we can start the task quickly with Ctrl+shift+b shortcut, and Problemmatcher matches the error output with a regular, so that the wrong line is marked with a wavy red. Finally started is the Vscgo_build script, I have only written under the Windows processing, Linux slightly changed on the line:

@rem param1 GOPATH@rem param2 build path@SET GOPATH=%1@CD %2@go build@rem succeed or failed@if %errorlevel%==0 (echo build success) else (echo build failed)

And the install is similar, just can't call out through the shortcut key, only through the call Out command panel, input tasks:run task to choose Vsc_install to do. The configured task is as follows:

{        "taskName": "vscgo_install",        "args": [            "${workspaceRoot}",            "${fileDirname}"        ],        "isWatching": false,        "problemMatcher": {            "owner": "go",            "fileLocation": [                "relative",                "${fileDirname}"            ],            "pattern": {                "regexp": "^(.*):(\\d+):\\s+(.*)$",                "file":1,                "line": 2,                "message": 3            }        }    }

The script is as follows:

@rem param1 GOPATH@rem param2 build path@SET GOPATH=%1@CD %2@go install@rem succeed or failed@if %errorlevel%==0 (echo install success) else (echo install failed)

So our approximate configuration task is done, and these things are already packaged in Git.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.