Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.Sign up
GitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
proposal: cmd/gofmt: organize renamed imports #26846
For example, the following import will be sorted alphabetically
import( "foo" "bar" "baz" "example.com/quux" "example.com/garply" "example.com/plugh" "example.com/xyzzy" "example.com/thud" corge "example.com/corge" grault "example.com/grault" waldo "example.com/waldo" fred "example.com/fred" )
which would result to an untidy mix
import( "bar" "baz" "foo" corge "example.com/corge" fred "example.com/fred" "example.com/garply" grault "example.com/grault" "example.com/plugh" "example.com/quux" "example.com/thud" waldo "example.com/waldo" "example.com/xyzzy" )
I proposed that aliased imports to be organized to it's own block, for example
import( "bar" "baz" "foo" "example.com/garply" "example.com/plugh" "example.com/quux" "example.com/thud" "example.com/xyzzy" corge "example.com/corge" fred "example.com/fred" grault "example.com/grault" waldo "example.com/waldo" )
So it would be much cleaner and easily readable.
If you have a bunch of related renamings then you may want to see them all together. If you have just a few in a large list of imports, the benefit of sorting them with the normal imports is that when you look for a particular import you only need to consider one list. For example in cmd/go we have variables named 'path' quite a bit, so conventionally the import of "path" comes in as pathpkg. Here's one real import block:
I think it would be inappropriate for gofmt or goimports to force moving the path import into its own section.
We intentionally leave this to local custom - gofmt and goimports will never move imports across blank lines in the import block. So if I want path with the others, I can do that, and gofmt/goimports respect that. And if you want all your renamed imports together, you can do that, and gofmt/goimports respect that too.
There's not a compelling reason to force everyone onto one side or the other.