Useful Go Command -1: goimports

Baris Eser
Apr 30, 2022

There’s an enhanced version of go fmt available called goimports that also cleans up your import statements. It puts them in alphabetical order, removes unused imports, and attempts to guess any unspecified imports. Its guesses are sometimes inaccurate, so you should insert imports yourself.

go install golang.org/x/tools/cmd/goimports@latestgoimports -l -w .

The -l flag tells goimports to print the files with incorrect formatting to the console.

The -w flag tells goimports to modify the files in-place.

The . specifies the files to be scanned: everything in the current directory and all of its subdirectories.

--

--