Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 533
Kaveh Shahbazian Avatar asked Nov 20 '15 13:11

Kaveh Shahbazian


People also ask

How use run on save VS Code?

If you don't already know how to, you can get there by going to File > Preferences > Settings or hitting Ctrl + ,(Cmd + , if you're on a Mac). Then click the Extensions tab on the left side menu, look for the extension Run On Save, and click Edit in settings. json. Run On Save settings page.

How do I run FMT?

If your version of GoLand 2021.2 or higher, you can enable Run gofmt on code reformat option under Preferences/Settings | Editor | Code Style | Go | Other (it is enabled by default). After that, execute Code | Reformat Code ( Option + Command + L on macOS) and gofmt will be executed after internal GoLand formatter.


1 Answers

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

like image 199
Yann C. Avatar answered Sep 22 '22 09:09

Yann C.