1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 addBuildFlagsNX(cmdFmt
)
16 var cmdFmt
= &Command
{
18 UsageLine
: "fmt [-n] [-x] [packages]",
19 Short
: "run gofmt on package sources",
21 Fmt runs the command 'gofmt -l -w' on the packages named
22 by the import paths. It prints the names of the files that are modified.
24 For more about gofmt, see 'go doc cmd/gofmt'.
25 For more about specifying packages, see 'go help packages'.
27 The -n flag prints commands that would be executed.
28 The -x flag prints commands as they are executed.
30 To run gofmt with specific options, run gofmt itself.
32 See also: go fix, go vet.
36 func runFmt(cmd
*Command
, args
[]string) {
38 for _
, pkg
:= range packages(args
) {
39 // Use pkg.gofiles instead of pkg.Dir so that
40 // the command only applies to this package,
41 // not to packages in subdirectories.
42 run(stringList(gofmt
, "-l", "-w", relPaths(pkg
.allgofiles
)))
46 func gofmtPath() string {
49 gofmt
+= toolWindowsExtension
52 gofmtPath
:= filepath
.Join(gobin
, gofmt
)
53 if _
, err
:= os
.Stat(gofmtPath
); err
== nil {
57 gofmtPath
= filepath
.Join(goroot
, "bin", gofmt
)
58 if _
, err
:= os
.Stat(gofmtPath
); err
== nil {
62 // fallback to looking for gofmt in $PATH