gofmt 和 go fmt 和有什么不一样?

cod*_*efx 10 go

我看到gofmt和都有go fmt。gofmt 和 go fmt 和有什么不一样?

Cer*_*món 16

运行go help fmt以查看差异。简而言之,在参数指定的包上go fmt运行gofmt -l -w

-w标志将结果写回源文件。该-l标志打印修改文件的名称。

的参数go fmt是包(运行go help packages描述)。的参数gofmt是文件系统路径。

以下是一些示例,显示了如何以不同方式处理参数:

 gofmt -w .   # formats files in current directory and all sub-directories
 go fmt ./... # similar to previous
 go fmt .     # formats files in current package
 gofmt -w foo/bar # formats files in directory $PWD/foo/bar and sub-dirs
 go fmt foo/bar   # formats files in directory $GOPATH/src/foo/bar
 gofmt -w     # error, no file or directory specified
 go fmt       # formats files in current package
Run Code Online (Sandbox Code Playgroud)


Lui*_*ird 3

gofmt 命令将处理作为参数给出的文件。go fmt 工具对作为参数给出的包路径中的所有文件运行 gofmt。因此,如果我在encoding/gob目录中,

gofmt解码.go

当工具运行时,将格式化单个文件decode.go

去吧。

(.实际上是默认值)将格式化encoding/gob包中的所有文件。