How to Configure Useful User Snippets in VSCode for Golang by@tiago-melo
385 reads

How to Configure Useful User Snippets in VSCode for Golang

tldt arrow
Read on Terminal Reader

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

@tiago-melo

Tiago Melo

Senior Software Engineer

About @tiago-melo
LEARN MORE ABOUT @TIAGO-MELO'S EXPERTISE AND PLACE ON THE INTERNET.

Credibility

react to story with heart

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":

image

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

image

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

image

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

image

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

image

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

image

Press "ENTER":

image

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?

RELATED STORIES

L O A D I N G
. . . comments & more!
Hackernoon hq - po box 2206, edwards, colorado 81632, usa