gofmt: sorting imports

3,014 views
Skip to first unread message

Albert Strasheim

unread,
Mar 23, 2011, 1:06:53 AM3/23/11
to golang-dev
Hello all

Does anybody have any thoughts on whether gofmt should or shouldn't
sort imports?

If it should, any thoughts on sort orders? I'd suggest sorting on path
and ignoring the name, but that might look a bit strange sometimes.

Thoughts?

Regards

Albert

Russ Cox

unread,
Mar 23, 2011, 1:11:28 AM3/23/11
to Albert Strasheim, golang-dev
> Does anybody have any thoughts on whether gofmt should or shouldn't
> sort imports?

It should, but it's difficult.

> If it should, any thoughts on sort orders? I'd suggest sorting on path
> and ignoring the name, but that might look a bit strange sometimes.

The standard order is to sort blocks by path, where a block
is a sequence of imports with no blank lines. So for example:

import (
"math"
"strconv"

"myproject/bar"
"myproject/foo"
)

The main reason gofmt doesn't do this reordering is that moving
code around doesn't fit well with gofmt's model for comments,
which are tagged with position information and reinserted in the
stream during output. That works very well most of the time
but I believe it will mishandle

import (
"strconv" // for Atoi
"math" // for Pi
)

Reordering the lines will not reorder the comments correctly.

Russ

Robert Griesemer

unread,
Mar 23, 2011, 12:29:06 PM3/23/11
to Albert Strasheim, golang-dev
Gofmt could and probably should sort the imports, a little while ago I
played with it, but I haven't found a satisfying solution yet.

Russ is correct that comments pose the main problem; however line
comments "attached" to the right of an import could probably be
handled.

Besides an alphabetic sorting, one may also preserve some grouping
(say std packages first, followed by others), etc. That's where it
gets difficult.

I'm happy to give it another try. Feel free to file an issue request.
It's not going to be high on my priority list though.
- gri

Sameer Ajmani

unread,
Mar 23, 2011, 7:47:29 PM3/23/11
to golang-dev
Would this sorting ignore the local names (if any) for the packages?

Robert Griesemer

unread,
Mar 23, 2011, 7:50:58 PM3/23/11
to Sameer Ajmani, golang-dev
On Wed, Mar 23, 2011 at 4:47 PM, Sameer Ajmani <ajm...@gmail.com> wrote:
> Would this sorting ignore the local names (if any) for the packages?

Depends what the sort criteria is. It makes sense that the import path
is used for sorting since it's less likely to change than the local
name. Also, people will be used to a sort order of import paths, and
not personal local names.

Anyway, nothing concrete has been proposed.
- gri

Nigel Tao

unread,
Mar 24, 2011, 12:56:55 AM3/24/11
to Sameer Ajmani, golang-dev
On 24 March 2011 10:47, Sameer Ajmani <ajm...@gmail.com> wrote:
> Would this sorting ignore the local names (if any) for the packages?

My personal style is to group renamed packages at the end of the
import block and sort on names rather than the import path. It's the
same effect as piping the import block through the Unix sort program.

Reply all
Reply to author
Forward
0 new messages