How to run `go fmt` on save, in Visual Studio Code?

Kaveh Shahbazian picture Kaveh Shahbazian · Nov 20, 2015 · Viewed 19.1k times · Source

How to make Visual Studio Code (or Go Programming Language Extension) to run go fmt (or other tools/commands) on save? Even auto save?

Update: It is working now perfectly inside VSCode, at this time; just need to add some config files inside .vscode directory (I use these).

Update 2019: This question is old. The VSCode Go extension has all you need to develop in Go, now.

Last Update 2019 BTW It worth mentioning that right above the package declaration inside your test files appears a run package tests. If you click it, you can see your code coverage of your code. The covered and not-covered parts are highlighted in different colors.

Update 2020 And now, the Go Extension for VSCode, is under the supervision of Go Team! 🎉

enter image description here

Answer

Yann C. picture Yann C. · Nov 28, 2018

Now, the feature has been implemented, you can enable format on save:

  1. Open Settings (Ctrl + ,)
  2. Search for editor.formatOnSave and set it to true

Your Go code will be formatted automatically on Ctrl + s