Go: x/tools/cmd/goimports: add -s flag

Created on 17 Sep 2014  路  10Comments  路  Source: golang/go

goimports claims to be a drop-in replacement for editor's gofmt-on-save hook and have
the same command-line interface as gofmt; but it does not support the -s flag.

-s flag can be useful in on-save hook unlike gofmt's -r flag.
FrozenDueToAge Thinking

Most helpful comment

I forked the tools repository and added this behavior if anyone is interested: go install github.com/jzelinskie/tools/cmd/goimports

I just copied the code from gofmt and removed the import comments: https://github.com/jzelinskie/tools/commit/348c54dbd0dc54b93cd91d71cfd483d55d00f971

If you're using vim-go you can add the following to your .vimrc to get both gofmt -s and goimports on save.

let g:go_fmt_command = "goimports"
let g:go_fmt_options = "-s"

All 10 comments

Comment 1:

_Labels changed: added repo-tools, release-none._

Comment 2:

It also doesn't support -r.
I think this is a documentation bug. We can say it's "almost a drop-in replacement".

Comment 3:

In which case, can we treat this as a feature request for -s? 
While -r is not very useful in an on-save hook, it'll be nice to have goimports as the
on-save command and include the -s flag.

Comment 4:

I ripped it out on purpose, to not duplicate code.
But if Robert is okay with moving it to a package in go.tools so simplify can be shared
by both gofmt and goimports, then I'm okay with it.

Comment 5:

I am not against it but gofmt (or go fmt) is not depending on anything but the std lib
at the moment.
Note that we are also considering removing that -r option from gofmt; so it may make
sense to remove the -s option as well and have a separate tool instead.
Not for 1.4 for sure.

_Status changed to Thinking._

This is causing some issues on my end - is this ever going to be considered? Or, at the least, the documentation needs to be updated.

I forked the tools repository and added this behavior if anyone is interested: go install github.com/jzelinskie/tools/cmd/goimports

I just copied the code from gofmt and removed the import comments: https://github.com/jzelinskie/tools/commit/348c54dbd0dc54b93cd91d71cfd483d55d00f971

If you're using vim-go you can add the following to your .vimrc to get both gofmt -s and goimports on save.

let g:go_fmt_command = "goimports"
let g:go_fmt_options = "-s"

Related: #14500

(Note FTR: per https://github.com/golang/tools/commit/7d47e840ac8ec63c4a5cd5c900156cad5e6b6a63, goimports is no longer claimed to be "a drop-in replacement [for gofmt]".)

@akavel, thanks.

I'll close this then.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

michaelsafyan picture michaelsafyan  路  3Comments

rsc picture rsc  路  3Comments

gopherbot picture gopherbot  路  3Comments

natefinch picture natefinch  路  3Comments

mingrammer picture mingrammer  路  3Comments