The Go extension runs the below on each file save by default. You can turn them off in settings.

  • Build (Turn off using go.buildOnSave setting)
  • Lint (Turn off using go.lintOnSave setting)
  • Vet (Turn off using go.vetOnSave setting)
  • Format (Turn off by adding the below in your settings)
"[go]": {
    "editor.formatOnSave": false
}

The below can be enabled to run on file save by turning them on in settings

  • Test
  • Code Coverage

Build on save

On each file save, the package that the file belongs to is built to find any compile errors. If the file is a test file then go test -i -c -o is used instead of go build -i -o

The binary generated by the build process is written to a temporary location as the purpose here is to only find compile errors and not to provide a binary/executable.

The below settings control the build on save feature

  • go.buildOnSave
    • Use this to either turn off the build on save feature or enable it at package or workspace level
  • go.buildTags
    • The tags provided here is used by the build and vet processes as well as any other Go tool used by this extension that takes the -tags flag
  • go.buildFlags
    • Is used both by the build and test processes
  • go.installDependenciesWhenBuilding
    • By default, -i flag is passed to speed up the build process. If you don't want the -i flag to be used, set go.installDependenciesWhenBuilding to false.

Lint on save

A linter is a tool giving coding style feedback and suggestions. By default this extension uses the official golint as a linter.

You can change the default linter and use the more advanced Go Meta Linter by setting go.lintTool to “gometalinter” in your settings.

Go meta linter uses a collection of various linters which will be installed for you by the extension.

Some of the very useful linter tools:

  • errcheck checks for unchecked errors in your code.
  • varcheck finds unused global variables and constants.
  • deadcode finds unused code.

If you want to run only specific linters (some linters are slow), you can modify your configuration to specify them:

  "go.lintFlags": ["--disable-all", "--enable=errcheck"],

Alternatively, you can use megacheck which may have significantly better performance than gometalinter, while only supporting a subset of the tools.

To disable lint on save turn off go.lintOnSave

Format on save

If you have Auto Save feature enabled, then you might want to disable the format on save feature so that the code doesnt keep changing under you. You can do this by adding the below in your settings:

"[go]": {
   "editor.formatOnSave": false
}

By default, goreturns is the tool used for formatting. You can choose goimports or gofmt by changing the go.formatTool setting

If you see your unused imports disappearing or unimported packages getting added automatically, thats the goreturns tool doing the magic behind the scenes.