Napat Rungruangbangchan

Golang Vscode Setup

Installation

  1. เปิด Vscode ขึ้นมาแล้วติดตั้ง extension ที่ชื่อว่า Go for Visual Studio Code ให้เรียบร้อย
  2. เปิด Command Palette (Cmd+Shift+P) เลือกไปที่ Install/Update Tools command และติดตั้ง tools ใน list ทุกตัว

vscode-gotools

Settings

สร้างไฟล์ .vscode/settings.json เข้าไปยัง root ของ project เพื่อกำหนด settings ให้กับ workspace

├── foo
    ├── .vscode
    │   └──settings.json
    ...

ตัวอย่าง settings.json

{
    // Formater: gofumpt 
    "editor.formatOnSave": true,
    "editor.tabSize": 2, // default tab size
    "editor.insertSpaces": true, // default: insert spaces instead of tabs
    // "editor.detectIndentation": false,
    "[go]": {
        "editor.defaultFormatter": "golang.go"
        "editor.tabSize": 4, // Go tab = 4 spaces
        "editor.insertSpaces": false, // https://pkg.go.dev/cmd/gofmt -> uses tabs for indentation
    },
    "go.useLanguageServer": true,
    "gopls": { 
        "ui.semanticTokens": true,
        "formatting.gofumpt": true
    },
    
    // Unit Test
    "go.coverOnSave": true,
    "go.coverOnSingleTest": true,
    "go.coverageDecorator": {
        "type": "gutter",
        "coveredBorderColor" :  "rgba(64,128,128,0.5)",
        "uncoveredBorderColor" :    "rgba(128,64,64,0.25)",
        "coveredHighlightColor": "rgba(64,128,128,0.5)",
        "uncoveredHighlightColor": "rgba(128,64,64,0.25)",        
        "coveredGutterStyle": "blockgreen",
        "uncoveredGutterStyle": "blockred"
    },
    "go.coverageOptions": "showBothCoveredAndUncoveredCode",
}

References