본문 바로가기
Go

[Go] Configure VSCode

by llHoYall 2020. 11. 28.

Currently, I am using VSCode. I've used it since the beta.

So I develop most of the language with VSCode.

This post describes the configuring development environment of Go in VSCode.

Extension

First, install an extension for Go.

If you search Go in the VSCode extension, you will see the two kinds of extensions.

Go is a stable version as an LTS, and Go Nightly is a preview version under development.

I chose an LTS version.

After installation, VSCode will ask you for installing other tools, just click the Install All.

Then, the binaries will be installed in the GOPATH and you will be able to enjoy all the convenience features.

Formatter

If you follow the guide above, the linter will be applied immediately.

Now, I want to automatically apply formatter when I save codes.

Put the below settings in the .vscode/settings.json of your workspace.

{
  "[go]": {
    "editor.formatOnSave": true,
  }
}

Run & Debug

This configuration allows running and debugging Go codes in VSCode.

Put the below settings in the .vscode/launch.json of your workspace.

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch",
      "type": "go",
      "request": "launch",
      "mode": "auto",
      "program": "${fileDirname}",
      "env": {},
      "args": []
    }
  ]
}

Try to press F5 key on Go codes you want to run and debug.

Ta Da~

You can also create a breakpoint into a code.

Build

If you want to build Go code, this configuration will help you.

Put the below settings in the .vscode/tasks.json of your workspace.

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "build",
      "type": "shell",
      "command": "go build ${file}",
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

Pressing <CTRL>+<SHIFT>+b, it builds a specified file.

Test

Like a build task, you can make a test task.

{
  // ...
    {
      "label": "test",
      "type": "shell",
      "options": {"cwd": "${fileDirname}"},
      "command": "go test",
      "group": {
        "kind": "test",
        "isDefault": true
      }
    }
  // ...
}

If you want to use it more conveniently, bind a key.

In my case, I registered like below on my Mac.

Add -cover option to command if you want to coverage data.

'Go' 카테고리의 다른 글

[Go] Operator  (0) 2021.10.04
[Go] File handling  (0) 2020.12.02
[Go] User-Defined Type  (0) 2020.11.02
[Go] Basic  (0) 2020.10.26
[Go] Variable  (0) 2020.10.21

댓글