Golang imports sorting/grouping hell and painless tool to solve it

For a long time of being working as Golang Dev I saw that a lot of my coleagues don’t pay attention on right sorting order of their dependencies(imports) in projects. I believe, it happens because of existed tools like goimports or even built-in IDE features can’t give the exact tool to do so. That’s why we should control and fix it manually when this situation is appeared and it is sad to perform such things which could be done automatically.

Let’s see on a practice, what do we have… In the example bellow we see that std libraries were split with whitespace on line #6.

This is default problem which we could get after merging two different branches, for example. Whitespace between std libraries will do our code at least ugly or sometimes miss-understandable especially on code review. Having such problem inside import block we can’t fix using existed Golang’s tools. More awful situation is next:

The mishmash breaking understanding about affiliation of each dependency, which library is local and which is a third-party. It looks like a draft but not like a code for production. By the convention we should split std, third-party and local dependencies separately.

Solution for such kind of problem is here:

What does the tool do and why it is better than others? Basically, using AST and internal rules for sorting it will totally rebuild `import` block and write changes to source file(by default it will write all the changes to source file, but if you need, you could print all of the changes to stdout using option -output stdout). Put source file for fixing with option -file-path of your project and that’s it.

Available options:

  -file-path string
File path to fix imports(ex.: ./reviser/reviser.go). Required parameter.
-project-name string
Your project name(ex.: github.com/incu6us/goimports-reviser). Optional parameter.
-local string
Local package prefixes which will be placed after 3rd-party group(if defined). Values should be comma-separated. Optional parameters.
-output string
Can be "file" or "stdout". Whether to write the formatted content back to the file or to stdout. Optional parameter. (default "file")
Remove unused imports. Optional parameter.
Set alias for versioned package names, like 'github.com/go-pg/pg/v9'. In this case import will be set as 'pg "github.com/go-pg/pg/v9"'. Optional parameter.

To install the tool on your system you could choose brew for Mac users, snap for linux users or natively for Go using go get.

Brew installation:

brew tap incu6us/homebrew-tap
brew install goimports-reviser

Snap installation:

snap install goimports-reviser

Using go get installation:

go get github.com/incu6us/goimports-reviser

Last part what you need to perform this is configuration inside your favourite IDE. Here it is an example with my favourite one using file watchers plugin:

Think, you’ll like the tool as a robust alternative to make your code better.

Happy coding!

Software Engineer