GoLang - Formatting and Styling

Auto format code

https://go.dev/blog/gofmt

To format a single file

gofmt -w yourfile.go

-w flag writes the formatted code back to the file. Replace yourfile.go with the actual file path.

To format all the files in the current project

gofmt -w .

Auto fix imports

https://pkg.go.dev/golang.org/x/tools/cmd/goimports

See the page for installation details

Run it with

goimports -w . (at project root)

or

goimports -w <file_or_directory>

Style guides

https://google.github.io/styleguide/go/decisions.html


Links to this note