I made my goimports

What you want to do, what you made

goimports best

goimports 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

But goimports puts in weird blank lines

As 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.

Created goimports to minimize blank lines

In 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).

How to make

The gosimports created this time was implemented by modifying the code of the original goimports.

The processing of goimports is roughly divided into three

1. Organize the packages to import

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.

2. Insert a blank line for each group (gosimports changed here)

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.

3. Run gofmt

Finally, apply gofmt to finish.

Modified the second process

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.

Digression

Derived OSS retains original license

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.

Gofmt is especially useful for code generation etc.

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.

Renovate and release-drafter are very useful

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.

I really like the name gos imports

gosimports = simpler + goimports. I like it quite a lot.

Recommended Posts

I made my goimports
I made my own language. (1)
I made my own AML
〇✕ I made a game
I made my dog "Monaka Bot" with LineBot
I made my own primitive static site generator
I made an Ansible-installer
I made my own parallel link robot (software version)
I made my own parallel link robot (mechanical edition)
I made blackjack with python!
I made a python text
I made a discord bot
I made COVID19_simulator with JupyterLab
I made Word2Vec with Pytorch
I made wordcloud with Python.
I made a surveillance camera with my first Raspberry PI.
When I made a Discord Bot, my classmates destroyed my computer
I made my own OSS because I wanted to contribute to it
I made a script to say hello at my Koshien
I made a C ++ learning site
[Python] I made my own library that can be imported dynamically
I made a Line-bot using Python!
I made a CUI-based translation script (2)
I made a wikipedia gacha bot
Python> I made a test code for my own external file
I made a fortune with Python.
I made a CUI-based translation script
I made an Angular starter kit
I made a daemon with Python
I installed Linux on my Mac
I made my own research tool using the legal API [Smart Roppo]
Since I made my own smart lock, I will summarize the difficult points
I put Alpine Linux in Larkbox and made it my home server
[C language] My locomotive is too slow ~ I made a sl command ~
I made a new AWS S3 bucket
I made something that moves (wider range)
I will talk about my first experience
I made a dash docset for Holoviews
I made Othello to teach Python3 to children (4)
I made a payroll program in Python!
I touched "Orator" so I made a note
I made a character counter with Python
I made Othello to teach Python3 to children (2)
I made an online frequency analysis app
I made an alternative module for japandas.DataReader
Beginner: I made a launcher using dictionary
I am making my own aws cli
I made Othello to teach Python3 to children (5)
I made a conversation partner like Siri
I made a script to display emoji
I made CORS custom middleware with Django
I made a Hex map with Python
I made a life game with Numpy
I made a stamp generator with GAN
I made a browser automatic stamping tool.
After studying Python3, I made a Slackbot
I made a roguelike game with Python
I made Othello to teach Python3 to children (3)
I installed OpenCV-Python on my Raspberry Pi
I tried my best to return to Lasso
I made a simple blackjack with Python