GoLang - Formatting and Styling
Table of Contents
Auto format code
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>