paint-brush
How to Configure Useful User Snippets in VSCode for Golangby@tiago-melo
1,510 reads
1,510 reads

How to Configure Useful User Snippets in VSCode for Golang

by Tiago MeloSeptember 24th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Table-driven tests have become my favorite way to write tests in [Golang] VSCode is simple, lightweight, and versatile. VSCodes is my favorite IDE nowadays: simple and lightweight. In this article, I'll show how we can configure some useful user snippets in VSCode to make our life easier. The following is an example for calculating Fibonacci: number: receiving number: problems for calculating number in V.SCode.
featured image - How to Configure Useful User Snippets in VSCode for Golang
Tiago Melo HackerNoon profile picture

VSCode is my favorite IDE nowadays: simple, lightweight, and versatile.


In this article, I'll show how we can configure some useful user snippets in VSCode to make our life easier.


Table-driven tests snippet

Since I read this Dave Cheney's article, table-driven tests have become my favorite way to write tests in Golang.


Here's an example for calculating Fibonacci number:


func TestRecursiveFibonacci(t *testing.T) {
    testCases := []struct {
        name string
        n    uint
        want uint
    }{
        {
            name: "zero",
            n:    0,
            want: 0,
        },
        {
            name: "one",
            n:    1,
            want: 1,
        },
        {
            name: "two",
            n:    2,
            want: 1,
        },
        {
            name: "three",
            n:    3,
            want: 2,
        },
    }
    for _, tc := range testCases {
        t.Run(tc.name, func(t *testing.T) {
            if got := RecursiveFibonacci(tc.n); got != tc.want {
                t.Errorf("got: %d, want: %d", got, tc.want)
            }
        })
    }
}


This is the output:


=== RUN   TestRecursiveFibonacci
=== RUN   TestRecursiveFibonacci/zero
=== RUN   TestRecursiveFibonacci/one
=== RUN   TestRecursiveFibonacci/two
=== RUN   TestRecursiveFibonacci/three
--- PASS: TestRecursiveFibonacci (0.00s)
    --- PASS: TestRecursiveFibonacci/zero (0.00s)
    --- PASS: TestRecursiveFibonacci/one (0.00s)
    --- PASS: TestRecursiveFibonacci/two (0.00s)
    --- PASS: TestRecursiveFibonacci/three (0.00s)
PASS
ok      bitbucket.org/tiagoharris/fibonacci/fibo    0.715s


It can be a bit boring writing this test structure every time. That's where predefined user snippets come to help.


Configuring a user code snippet in VSCode

Open up VSCode. If you're using macOS, hit command + shift + p and start typing "snippet":


After pressing ENTER, type "go" and select "go.json":



Then, ENTER again and it will open "go.json":



Here's where we'll define a snippet for generating the table-driven test basic structure:


"Table driven test": {
        "prefix": "tabletest",
        "body": [
          "func Test${1:YourFunc}(t *testing.T) {",
          "\ttestCases := []struct{",
          "\t\tname string",
          "\t}{",
          "\t\t{",
          "\t\t\tname: \"happy path\",",
          "\t\t},",
          "\t}",
          "\tfor _, tc := range testCases {",
          "\t\tt.Run(tc.name, func(t *testing.T) {",
          "\t\t})",
          "\t}",
          "}"
        ],
        "description": "Create basic structure for a table driven test"
 }


  • "Table driven test": a descriptive name for our snippet;
  • "prefix": how can we invoke this snippet? In our case, every time we type "tabletest" in any golang file (*.go), the structure will be written into it;
  • "body": the snippet itself. I'm using '\t' for tabbing. If you want to enter a new line, just type in '\n';
  • "func Test${1:YourFunc}(t *testing.T) {": here I'm using "${1:YourFunc}" so the cursor will be positioned around "YourFunc";
  • "description": a friendly description of this snippet.


Invoking it



When you start typing "table", VSCode will suggest our snippet. Press ENTER:



Notice that the cursor is around "YourFunc", so you can type your test name immediately.


Main function snippet

Here's another snippet that I find useful: it defines a basic structure for a file with the main function:


"Main Func": {
        "prefix": "mf",
        "body": [
          "package main\n",
          "import (",
          "\t\"fmt\"", 
          "\t\"os\"",
          ")\n",
          "func run() error {",
          "\treturn nil",
          "}\n",
          "func main() {",
          "\tif err := run(); err != nil {",
          "\t\tfmt.Println(err)",
          "\t\tos.Exit(1)",
          "\t}",
          "}",
        ],
        "description": "Create basic structure for a script with main function"
}


Invoking it



Press "ENTER":



Then a basic structure for a file with the main function will be written.


Our final "go.json" is the following:


{
    "Table driven test": {
        "prefix": "tabletest",
        "body": [
          "func Test${1:YourFunc}(t *testing.T) {",
          "\ttestCases := []struct{",
          "\t\tname string",
          "\t}{",
          "\t\t{",
          "\t\t\tname: \"happy path\",",
          "\t\t},",
          "\t}",
          "\tfor _, tc := range testCases {",
          "\t\tt.Run(tc.name, func(t *testing.T) {",
          "\t\t})",
          "\t}",
          "}"
        ],
        "description": "Create basic structure for a table driven test"
    },
    "Main Func": {
        "prefix": "mf",
        "body": [
          "package main\n",
          "import (",
          "\t\"fmt\"", 
          "\t\"os\"",
          ")\n",
          "func run() error {",
          "\treturn nil",
          "}\n",
          "func main() {",
          "\tif err := run(); err != nil {",
          "\t\tfmt.Println(err)",
          "\t\tos.Exit(1)",
          "\t}",
          "}",
        ],
        "description": "Create basic structure for a script with main function"
    }
}


Cool, isn't it?