I'm using vscode
with Go
extensions to edit golang
source code. Each time I want to format code, I have to press Ctrl-Shift-i
on linux
, Shift-Alt-F
on Windows
, or Ctrl-Shift-p
and type format
. Is it possible to set format on save, that is, when I press ctrl-s
, it format the code automatically using go.fmt
(or something alike)?
To automatically format the file on save, In Visual Studio Code, press Control + Shift + P or Command + Shift + P (Mac) to open the command palette and type setting and then select Preferences: Open User Settings option. Search for format on save setting and check the checkbox.
VS Code has great support for source code formatting. The editor has two explicit format actions: Format Document (Ctrl+Shift+I) - Format the entire active file. Format Selection (Ctrl+K Ctrl+F) - Format the selected text.
TIL how to save a file on VSCode without auto formatting being applied: CMD + K S . This can be useful in situations where one is working in a repository and is not interested in having a big file auto formatted, just for the sake of a trivial change.
You should install this plugin: https://github.com/golang/vscode-go. One of the options is to set "auto format" on save: go.formatOnSave": false
. It uses the Golang tooling for formatting.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With