goimports flag parity with gofmt

647 views
Skip to first unread message

Vasiliy Tolstov

unread,
Feb 20, 2014, 10:49:39 PM2/20/14
to golan...@googlegroups.com
As i see, sometimes i need to get gofmt -s, is that possible to add
this and other options to goimports?


goimports --help
usage: goimports [flags] [path ...]
-comments=true: print comments
-d=false: display diffs instead of rewriting files
-e=false: report all errors (not just the first 10 on different lines)
-l=false: list files whose formatting differs from goimport's
-tabs=true: indent with tabs
-tabwidth=8: tab width
-w=false: write result to (source) file instead of stdout

gofmt --help
usage: gofmt [flags] [path ...]
-comments=true: print comments
-cpuprofile="": write cpu profile to this file
-d=false: display diffs instead of rewriting files
-e=false: report all errors (not just the first 10 on different lines)
-l=false: list files whose formatting differs from gofmt's
-r="": rewrite rule (e.g., 'a[b:len(a)] -> a[b:]')
-s=false: simplify code
-tabs=true: indent with tabs
-tabwidth=8: tab width
-w=false: write result to (source) file instead of stdout


--
Vasiliy Tolstov,
e-mail: v.to...@selfip.ru
jabber: va...@selfip.ru

Brad Fitzpatrick

unread,
Feb 21, 2014, 12:29:42 AM2/21/14
to Vasiliy Tolstov, golang-nuts
You're running old binaries of each.



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Vasiliy Tolstov

unread,
Feb 21, 2014, 12:32:16 AM2/21/14
to Brad Fitzpatrick, golang-nuts
2014-02-21 9:29 GMT+04:00 Brad Fitzpatrick <brad...@golang.org>:
> You're running old binaries of each.


Hmm. I'm try new goimports but nothing different:

go get -v -x -u code.google.com/p/go.tools/cmd/goimports
code.google.com/p/go.tools (download)
cd /home/vtolstov/devel/go/src/code.google.com/p/go.tools
hg pull
cd /home/vtolstov/devel/go/src/code.google.com/p/go.tools
hg tags
cd /home/vtolstov/devel/go/src/code.google.com/p/go.tools
hg branches
cd /home/vtolstov/devel/go/src/code.google.com/p/go.tools
hg update default
WORK=/tmp/go-build706264051
code.google.com/p/go.tools/imports
mkdir -p $WORK/code.google.com/p/go.tools/imports/_obj/
mkdir -p $WORK/code.google.com/p/go.tools/
cd /home/vtolstov/devel/go/src/code.google.com/p/go.tools/imports
/usr/lib64/go/pkg/tool/linux_amd64/6g -o
$WORK/code.google.com/p/go.tools/imports/_obj/_go_.6 -p
code.google.com/p/go.tools/imports -complete -D
_/home/vtolstov/devel/go/src/code.google.com/p/go.tools/imports -I
$WORK -I /home/vtolstov/devel/go/pkg/linux_amd64 ./fix.go ./imports.go
./sortimports.go ./zstdlib.go
/usr/lib64/go/pkg/tool/linux_amd64/pack grcP $WORK
$WORK/code.google.com/p/go.tools/imports.a
$WORK/code.google.com/p/go.tools/imports/_obj/_go_.6
mkdir -p /home/vtolstov/devel/go/pkg/linux_amd64/code.google.com/p/go.tools/
cp $WORK/code.google.com/p/go.tools/imports.a
/home/vtolstov/devel/go/pkg/linux_amd64/code.google.com/p/go.tools/imports.a
code.google.com/p/go.tools/cmd/goimports
mkdir -p $WORK/code.google.com/p/go.tools/cmd/goimports/_obj/
mkdir -p $WORK/code.google.com/p/go.tools/cmd/goimports/_obj/exe/
cd /home/vtolstov/devel/go/src/code.google.com/p/go.tools/cmd/goimports
/usr/lib64/go/pkg/tool/linux_amd64/6g -o
$WORK/code.google.com/p/go.tools/cmd/goimports/_obj/_go_.6 -p
code.google.com/p/go.tools/cmd/goimports -complete -D
_/home/vtolstov/devel/go/src/code.google.com/p/go.tools/cmd/goimports
-I $WORK -I
/home/vtolstov/devel/go/pkg/linux_amd64 ./doc.go ./goimports.go
/usr/lib64/go/pkg/tool/linux_amd64/pack grcP $WORK
$WORK/code.google.com/p/go.tools/cmd/goimports.a
$WORK/code.google.com/p/go.tools/cmd/goimports/_obj/_go_.6
cd .
/usr/lib64/go/pkg/tool/linux_amd64/6l -o
$WORK/code.google.com/p/go.tools/cmd/goimports/_obj/exe/a.out -L $WORK
-L /home/vtolstov/devel/go/pkg/linux_amd64
$WORK/code.google.com/p/go.tools/cmd/goimports.a
mkdir -p /home/vtolstov/bin/
cp $WORK/code.google.com/p/go.tools/cmd/goimports/_obj/exe/a.out
/home/vtolstov/bin/goimports

/home/vtolstov/bin/goimports --help
usage: goimports [flags] [path ...]
-d=false: display diffs instead of rewriting files
-e=false: report all errors (not just the first 10 on different lines)
-l=false: list files whose formatting differs from goimport's

Brad Fitzpatrick

unread,
Feb 21, 2014, 12:35:18 AM2/21/14
to Vasiliy Tolstov, golang-nuts
I don't think you're running the correct binary then, because:

$ rm $(which goimports)
$ goimports -h
usage: goimports [flags] [path ...]
  -d=false: display diffs instead of rewriting files
  -e=false: report all errors (not just the first 10 on different lines)
  -l=false: list files whose formatting differs from goimport's
  -w=false: write result to (source) file instead of stdout

Brad Fitzpatrick

unread,
Feb 21, 2014, 12:36:59 AM2/21/14
to Vasiliy Tolstov, golang-nuts
Oh, you did update goimports.

gofmt and goimports both had their tab options removed.

But yes, goimports doesn't have cpuprofile, -r, or -s.  Not sure how much it matters.  No editors really use those, do they?  If you want to do it by hand, gofmt is still there.

ser...@google.com

unread,
Jun 12, 2017, 12:28:54 PM6/12/17
to golang-nuts, v.to...@selfip.ru
This is still bothering us in 2017. I'd love to be able to run goimports on save in Vim, but also would like to simplify code (-s option). I use vim-go plugin, so I guess I can submit a pull request asking the plugin to run both commands on save, but I do not understand why the workaround instead of command parity? Is it something no one has had time to look into yet or is it too hard to implement with current goimports implementation?

Brad Fitzpatrick

unread,
Jun 12, 2017, 12:56:06 PM6/12/17
to ser...@google.com, golang-nuts, Vasiliy Tolstov
Use the tool that does what you want.

We don't have to put all functionality into all binaries.

If vim-go makes assumptions that one helper binary does all functionality, yes, please fix vim-go.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chandru

unread,
Jun 12, 2017, 1:10:50 PM6/12/17
to Brad Fitzpatrick, ser...@google.com, golang-nuts, Vasiliy Tolstov
The documentation of goimports[1] says,

It's a drop-in replacement for your editor's gofmt-on-save hook. "It has the same command-line interface as gofmt" and formats your code in the same way. (quotes are mine)

I think this documentation needs fixing too?


--
Chandra Sekar.S

Brad Fitzpatrick

unread,
Jun 12, 2017, 1:24:03 PM6/12/17
to Chandru, ser...@google.com, golang-nuts, Vasiliy Tolstov

Fatih Arslan

unread,
Jun 13, 2017, 9:00:58 AM6/13/17
to Brad Fitzpatrick, ser...@google.com, golang-nuts, Vasiliy Tolstov
I think there is a misunderstanding here. People somehow don't read the manuals or the changelog when I release a new version. We have the following setting for  (which was released recently with v1.13):

  let g:go_fmt_options = {
    \ 'gofmt': '-s',
    \ 'goimports': '-local mycompany.com',
    \ }

Everyone who can see this immediately can see how this already solves this problem. As you see, vim-go can use different kind of options for each different binary name.


To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--
Fatih Arslan

Sergiy Byelozyorov

unread,
Jun 13, 2017, 9:07:18 AM6/13/17
to Fatih Arslan, Brad Fitzpatrick, golang-nuts, Vasiliy Tolstov
Yes, but what I need is to run "goimports" and then "gofmt -s". If goimports supported -s, then I would be able to do what I need, but not with current vim-go implementation.
--
Sergiy Byelozyorov | Software Engineer | ser...@google.com

Google Germany GmbH
Erika-Mann-Strasse 33
80636 München

AG Hamburg, HRB 86891 | Sitz der Gesellschaft: Hamburg | Geschäftsführer: Matthew Scott Sucherman, Paul Terence Manicle

Fatih Arslan

unread,
Jun 13, 2017, 9:38:59 AM6/13/17
to Sergiy Byelozyorov, Brad Fitzpatrick, golang-nuts, Vasiliy Tolstov
No you can do it. We have two commands, called :GoImports and :GoFmt. You can set the new setting and then call first :GoImports and then :GoFmt. With a little vim script you could create a single command that executes them in order. 
--
Fatih Arslan

Sergiy Byelozyorov

unread,
Jun 13, 2017, 11:19:13 AM6/13/17
to Fatih Arslan, Brad Fitzpatrick, golang-nuts, Vasiliy Tolstov
Ah. Good point. Added

autocmd BufWritePre *.go :GoFmt
autocmd BufWritePre *.go :GoImports

to my .vimrc. Thanks.

ramya...@gmail.com

unread,
Dec 17, 2017, 9:06:46 AM12/17/17
to golang-nuts
Brad,

Would you be open to consider a PR to add the "-s" flag support to goimports?
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

Brad Fitzpatrick

unread,
Dec 17, 2017, 10:54:21 AM12/17/17
to ramya...@gmail.com, golang-nuts
I'd still rather not.

But it'd be more than one change anyway. We're not just going to copy/paste the code from gofmt into goimports if it did happen. We'd want to move the simplify code to an x/* package (either https://godoc.org/golang.org/x/tools/go/ast/astutil or a subdirectory thereof) and then use it from both goimports and gofmt, which would additionally require vendoring the simplify package into std.




To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages