goimports
bestgoimports
is Go's CLI tool that does a ↓ on a * .go
file.
--It will import
external packages that have not been imported
.
--Unused import
will be deleted
――Finally, it also plays gofmt
goimports
puts in weird blank linesAs you can see in the issue of github.com/golang/go/issues/20818, goimports
puts in weird blank lines.
The reason for this is simply "the specifications have not been decided", and it has been discussed in Issue for about four years.
goimports
to minimize blank linesIn such a situation, I thought that I would have no choice but to make my own goimports
.
https://github.com/rinchsan/gosimports
No matter how many blank lines you put in or the order is out of order, no questions are asked and the number of blank lines is minimized.
In other words, put only one blank line between the standard package and the rest (you can also put a blank line before the package in the project by using the -local
option like the original one).
Even if you write a comment in the import
block, it will be deleted without asking questions (I would like to improve this if there is a nice specification).
The gosimports
created this time was implemented by modifying the code of the original goimports
.
I'm statically analyzing the package I'm using by looking at the contents of the * .go
file.
In the source code of the head family, it is around here.
This is not modified at all in this self-made tool.
gofmt
sorts the packages separated by blank lines in the import
block into Alphabetical order.
So, in order to make the original goimports
as clean as possible (?), The process of inserting a blank line between the standard package and other parts is performed beforegofmt
.
In terms of source code, it is around here.
Finally, apply gofmt
to finish.
In the gosimports
that I made this time, the second process of ↑ is modified.
In terms of source code, it is around here.
The addImportSpaces
function that was in the original home has been changed to the separateImportsIntoGroups
function.
For the grouping of import
(standard package and others), Classification function was already implemented in the head family, so I used it.
Go's tools are probably published as OSS under a license called the BSD license, as is goimports
.
For projects released under the BSD license, you can freely modify and redistribute the source as long as you do not use the name of the original author for promotion while keeping the license as it is.
The gos imports
created this time also retains the original license. OSS is the best.
The part that generates the code of gosimports
made this time is made quite appropriately.
There are no tab characters or useless blank lines.
But the gofmt
running later on cleans everything up, so it was very easy to implement.
I've always been indebted to gofmt
, but I found it especially useful when implementing code generation.
The only packages that gosimports
depends on are golang.org/x/mod
and golang.org/x/tools
, but golang.org/x/tools
is git tagged. No release will only update the latest commit hash.
Renovate supports not only tag releases but also commit hash updates, so you can keep up with the latest.
Also, release-drafter, which adds to the release notes every time the PR is merged into the master branch, was also very useful.
gosimports = simpler + goimports
.
I like it quite a lot.
Recommended Posts