With gofmt, you can format Go source code in the opened file or in the whole Go project.
For a single file, you can use the built-in import management and code formatter. To reformat code, press Ctrl+Alt+L. Imports are managed on-the-fly. To customize import management, open settings by pressing Ctrl+Alt+S and navigate to.
Alternatively, you can use file watchers to trigger gofmt and goimports. File watcher is a built-in IntelliJ IDEA tool that allows you to run command-line tools automatically when files are changed or saved. IntelliJ IDEA automatically saves changes that you make in your files.
For example, to set a file watcher to run gofmt, open settings by pressing Ctrl+Alt+S and navigate to Add button and select gofmt. In the New Watcher dialog, click OK., click the
To format code in the opened file, go to.
To format code in the current project, go to.
To format code before you commit your changes into VCS, select the Go fmt checkbox. For more information about committing your changes, see Commit and push changes to Git repository.
With goimports, you can automatically update your Go import lines (add missing and remove unreferenced imports).
If your project does not have goimports, click the go get goimports link in the Goimports file notification window. Otherwise, open the Terminal tool window ( ), and type the following command to install goimports:
go get golang.org/x/tools/cmd/goimports. Press Enter.
To add missing imports, navigate to Goimports file.and click
go generate uses the
//go:generate directive to define and invoke the code generation. For more information about
go generate, see Generating code at blog.golang.org.
Add the following line to your Go code:
//go:generate command arguments(for example,
//go:generate stringer -type=Pill)
Click Run icon ( ) on the gutter and select Go Generate.. Alternatively, click the
go vet command examines Go source code and reports suspicious constructs.
To find possible bugs or suspicious constructs with
go vet, click . You can see the result of code analysis in the Terminal tool window.