Use gci in Goland as a file watcher

gci is an amazing tool. It takes the guesswork out of organising your go file’s imports, and the command line version works as expected. Here's how to set it up in Goland.

Use gci in Goland as a file watcher

gci is an amazing tool. It takes the guesswork out of organising your go file’s imports, and the command line version works as expected.

Here’s the settings that will work, and then a bit of explanation following:

Screenshot of a file watcher setting in Goland, File Watchers. Contents follow in text.

Give it whatever name. File type should be all Go files. I usually scope it to project files as I don’t want to change vendored files for example.

The program should be a full path to the gci executable. This is where you have installed it. A which gci will tell you what to put there.

For the arguments, the -w will tell gci to rewrite the file rather than output what the file should be to stdout. The -local part is followed by the import path that we want to consider local to the project. In most cases this is going to be github.com/organization-name/project-name, though occasionally it’s just the organization, without the project.

Lastly we need to tell gci which file to check, so we add the $FilePath$ tag, which expands onto the full path of whichever file you’re currently running the File watcher on, i.e. saving.

The output paths to refresh is empty. Normally this should be $FilePath$ as well, but for whatever reason Goland does not pick up the changes to resave if I have the file path there, and I remove the -w flag.

I don’t know why.

More info on Goland’s help pages on File Watchers.

That’s all I had for today. Enjoy your midweek, folks!

Photo by Martin Sanchez on Unsplash