gofmt is different from go fmt?

365 views
Skip to first unread message

Jon Renner

unread,
May 26, 2013, 5:09:16 AM5/26/13
to golan...@googlegroups.com
I was under impression that "go fmt" and "gofmt" were aliases for the same command.  Recently I discovered they have somewhat different behaviour. for example: "go fmt -h" vs "gofmt -h" is rather confusing.

Jsor

unread,
May 26, 2013, 5:31:02 AM5/26/13
to golan...@googlegroups.com
It's not a straight alias, no. Gofmt requires a (list of same-directory) .go file arguments. By default, it also doesn't overwrite the file passed in, but merely prints the output to stdout. 

Go fmt will run gofmt -w -l (rewrite the file with gofmt's output unless there's an error, list the files that changed) on either a single file, list of files in the same directory, or package (relative to $GOSRC). Further, if you use go fmt you can't specify additional options such as -r (rewrite rule) or -s (simplify).

Generally speaking, go fmt is more useful in my experience except in specific cases (like abusing the rewrite rule for codegen, or telling travis-cl to fail if the committed code wasn't formatted correctly).

Konstantin Kulikov

unread,
May 27, 2013, 7:56:50 AM5/27/13
to golan...@googlegroups.com
Have a look at src/cmd/go/fmt.go
Reply all
Reply to author
Forward
0 new messages