Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running 'goimports' on save in GoLand

Tags:

go

goland

I'm using GoLand IDE to code in Go. When I save a file, I'd like that the unused imports disappear (like VS code)

I have enable "Optimize imports" in "Code" -> "Show reformat file dialog". But when I save the file, imports it's still present

like image 959
matiux Avatar asked Aug 09 '17 12:08

matiux


People also ask

How do I run a Goimports file?

goimports 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 (View | Tool Windows | Terminal), and type the following command to install goimports: go get golang.org/x/tools/cmd/goimports . Press Enter .

How do I run a go format?

If your version of GoLand 2021.2 or higher, you can enable Run gofmt on code reformat option under Preferences/Settings | Editor | Code Style | Go | Other (it is enabled by default). After that, execute Code | Reformat Code ( Option + Command + L on macOS) and gofmt will be executed after internal GoLand formatter.

How do I use the Go tools in Goland?

Currently, GoLand offers three ways to interact with ‘go fmt’: dedicated actions, before commit tools, and file watchers. Under the Tools | Go Tools menu, you can find two actions related to code formatting: Go Fmt File and Go Fmt Project.

How do I use goimports in go?

When you use this grouping, GoLand puts all imports that begin with the specified string after third-party packages. Open settings by pressing Ctrl+Alt+S and navigate to Editor | Code Style | Go. Click the Imports tab. From the Sorting type list, select goimports.

How do I import Golang code in terminal?

Otherwise, open the Terminal tool window (View | Tool Windows | Terminal), and type the following command to install goimports: go get golang.org/x/tools/cmd/goimports. Press Enter. To add missing imports, navigate to Tools | Go Tools and click Goimports file. The goimports tool also formats your code in the same style as gofmt.

Why modify file content in Goland?

If you’ve never used GoLand before, you may be surprised that there is more than one internal reason to modify file content. Indeed, conventional text editors typically have a straightforward workflow. You open a file and modify it, and then you either explicitly save it or close the editor, losing your changes.


2 Answers

As of version 2021.3

Two separate entries were added to the File Watchers that are enabled by default. One for formatting the code on save, one for optimising the imports on save:

enter image description here

How the imports are sorted, grouped, etc. can be further defined in the Code Style settings for Go:

enter image description here

There is another small option to run gofmt on reformat in the Other tab of Go's Code Style settings:

enter image description here

Before version 2021.3

The setting to set up gofmt or goimports on save is in the File Watchers. Here you can add a new file watcher at the bottom and select gofmt or goimports from the list.

enter image description here

Setting this to goimports will do both:

  • code formatting (gofmt)
  • updating, sorting and grouping imports
like image 159
TehSphinX Avatar answered Oct 20 '22 23:10

TehSphinX


"On Save" tab was removed in GogLand EAP 16.

In newer versions you have to go to Settings -> Tools -> File Watchers and add goimports there.

like image 10
Elon Than Avatar answered Oct 21 '22 01:10

Elon Than