I struggle to write code.
Not because of the complexity of the challenge I’m trying to solve. Ha! If only that were the case.
When I write code, it definitely has to be haute. So beautiful, elegant, and downright pretty you forget why you were looking at it it’s so pretty.
One of my own personal nitpick tendencies is import sorting in Go. Any time I clone a new repo or contribute to a project, generally my first commit looks something like this:
When a friend of mine posted this question on twitter, I started to look at my own projects and tools for an answer.
Normally, go projects set up the usual
go fmt either using vim-go or a File Watcher for one of the IntelliJ products.
Here’s an example of the
go fmt File Watcher from Goland:
What this does, is when a file is edited and subsequently saved Goland will immediately run
go fmt on that file in order to cleanup any erroneous spacing or improperly formatted code.
Why isn’t this enough?
You might be thinking, “but doesn’t
go fmt sort imports?” Yes, yes it does. But if you have core, external, and internal packages all bunched together it will merely sort them alphabetically.
Ignoring the fact that I’m not willing to muddy up a
go.mod for a medium article, here’s an example of the type of sorting
go fmt does for you:
How can I make it haute?
Luckily, Goland already has a built in
goimports File Watcher so it’s much simpler than you think.
Here’s an example
goimports File Watcher configuration on a real project i’m currently working on:
The only real bit of customization here is the addition of
-local github.nande.com to the arguments line of
This configuration produces haute imports for a given Go file, again ignoring the fact that I’m not willing to muck about with a perfectly good
One of the hidden features of
goimport is that it will also
fmt your code under the hood so you can choose to standardize solely on
goimports for your projects and get the best of both worlds.
This is just the tip of the haute iceberg. Stay tuned for more stories like this to really round out the Titanic-ness of super haute code.