Go tools
Example
IntelliJ IDEA integrates essential Go tools to help you write, format, analyze, and generate code more efficiently. This topic shows you how to use the most popular Go tools inside the IDE.
IntelliJ IDEA includes built-in import management and a code formatter. Imports are handled on the fly. To customize how imports are processed, open settings by pressing CtrlAlt0S and navigate to Languages & Frameworks | Go | Imports.
To reformat code, press CtrlAlt0L. Unlike gofmt
, the IntelliJ IDEA formatter works with incomplete or syntactically incorrect code and can be applied to selected blocks. It also supports advanced formatting options such as inserting semicolons automatically, wrapping parameters, and more.
To run both formatters at once, enable the On Reformat Code action option under Editor | Code Style | Go on the Other tab.
You can also enable Reformat code in Actions on Save. This option is enabled by default. When you press Ctrl0S, the IDE runs both the built-in formatter and gofmt
.
Use gofmt
to format Go source code in the current file or across the whole project.
To format code in the currently opened file, navigate to Tools | Go Tools | Go Fmt File.
To format all code in the project, navigate to Tools | Go Tools | Go Fmt Project.
To format code before committing changes to VCS, select the Go fmt checkbox in the Commit dialog. For more information, refer to Commit and push changes to Git repository.
tip
To learn more, refer to Command gofmt at pkg.go.dev.
IntelliJ IDEA includes built-in import management and a code formatter. Imports are handled on the fly. To customize how imports are processed, open settings by pressing CtrlAlt0S and navigate to Languages & Frameworks | Go | Imports.
Use goimports
to automatically add missing and remove unused imports.
To apply
goimports
, navigate to Tools | Go Tools | Goimports File.
If goimports
is not available in your project, click the go install goimports
link in the notification. Alternatively, open the Terminal tool window (View | Tool Windows | Terminal) and run:
go install golang.org/x/tools/cmd/goimports@latest
tip
goimports
also formats code in the same way asgofmt
. For more details, see Command goimports at pkg.go.dev.
Use the go generate
command and the //go:generate
directive to run code generation tools.
Add a directive to your code:
//go:generate command arguments
For example:
//go:generate stringer -type=Pill
Run
go generate
from the IDE:Tools | Go Tools | Go Generate File
Or use the gutter icon
and select Go Generate
tip
For more details, refer to Generate Go files by processing source at pkg.go.dev and Generating code at go.dev.
Use go vet
to identify possible bugs and suspicious constructs in Go source files.
To run
go vet
, navigate to Tools | Go Tools | Go Vet File. The output will appear in the Terminal tool window.
tip
To learn more, refer to Command vet at pkg.go.dev.
Thanks for your feedback!