go version go1.8.3 darwin/amd64
Using goimports as of https://github.com/golang/tools/commit/84a35ef54dff3c5596983e180ec10919fc432242
The goimports command handles import fixing and gofmt code formatting. It does not, however, implement the code simplification option -s.
This means that goimports cannot be used as a drop-in replacement to save-hooks that use gofmt -s (for example, when using -s as the gofmt-args value for the save-hook with go-mode.el).
package main
import "fmt"
type Thing struct {
value int
}
func main() {
things := []Thing{
Thing{value: 42},
}
fmt.Printf("things: %+v\n", things)
}
There are no problems identified by goimports:
$ goimports -d complex.go
With gofmt -s, we get:
$ gofmt -s -d complex.go
diff complex.go gofmt/complex.go
--- /var/folders/ts/s940qvdj5vj1czr9qh07fvtw0000gn/T/gofmt822245016 2017-08-16 12:34:31.000000000 -0400
+++ /var/folders/ts/s940qvdj5vj1czr9qh07fvtw0000gn/T/gofmt235716375 2017-08-16 12:34:31.000000000 -0400
@@ -8,7 +8,7 @@
func main() {
things := []Thing{
- Thing{value: 42},
+ {value: 42},
}
fmt.Printf("things: %+v\n", things)
}
Comment From: griesemer
FWIW, it's a historical artifact that the simplify (-s) mechanism is implemented as part of the gofmt command. I think AST simplification (and rewriting) should be factored out and provided via separate libraries. Then those libraries could be trivially used from other places (incl. gofmt or goimports if so desired).
Comment From: mbyio
@griesemer So basically, to implement this, someone should first factor out the simplification code and then use that to implement simplification in goimports?
Comment From: griesemer
@mbyio Yes.
Comment From: jimeh
@griesemer isn't the point of goimports that it's a drop-in replacement for gofmt?
Comment From: griesemer
@jimeh goimports already does more than gofmt. I'm just suggesting that simplifications such as this should be factored out into a separate library. Maybe that library is used by gofmt and goimports, or just goimports. But the main point is that the code should be isolated into a separate package.
Comment From: thockin
We just stumbled on this -- Kubernetes codegen tools use golang.org/x/tools/imports.Process() but our CI checks gofmt -s, which means we need to jump through some hoops to do the simplification that the -s does for us.
I don't suppose anything has changed here since January of 2019?