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