整个源代码树上运行Go fmt

huangapple go评论88阅读模式
英文:

Go fmt on a whole source tree

问题

我目前有一个项目的组织结构如下:

~/code/go
/bin
/pkg
/src
/proj/main.go
/some_package/package.go
/some_other_package/some_other_package.go

现在,如果我想在整个项目上使用go fmt工具,似乎唯一的方法是分别对项目源代码树中的每个目录进行操作:

go fmt proj
go fmt proj/package
go fmt proj/some_other_package

有没有办法告诉fmt命令在整个源代码树上运行?

英文:

I have a project currently organized something like this:

<pre>
~/code/go
/bin
/pkg
/src
/proj/main.go
/some_package/package.go
/some_other_package/some_other_package.go
</pre>

Now if I want to use the go fmt tool on my whole project it seems that the only way is to do it separately for each directory in my projects source tree:

go fmt proj
go fmt proj/package
go fmt proj/some_other_package

Is there some way to tell the fmt command to run on the whole source tree?

答案1

得分: 140

你可以使用三个点(...)作为通配符。所以例如,下面的命令将格式化所有的 github.com 包:

go fmt github.com/...

这个通配符也适用于其他的 go 命令,比如 go listgo get 等等。没有必要记住这样一个丑陋的查找命令。

英文:

You can use three dots (...) as a wildcard. So for example, the following command will format all github.com packages:

go fmt github.com/...

This wildcard also works with other go commands like go list, go get and so. There is no need to remember such an ugly find command.

答案2

得分: 92

如果你使用gofmt而不是go fmt,它是递归的。例如,以下命令

gofmt -s -w .

(注意末尾的小点)递归地格式化、简化并保存当前目录下的每个文件的结果。我定义了一个名为gf的shell别名,其定义为gofmt -s -w .,我觉得非常方便。

如果你想要的话,先尝试gofmt -l .列出与gofmt格式不同的文件整个源代码树上运行Go fmt

英文:

If you use gofmt instead of go fmt, it's recursive. For example, following command

gofmt -s -w .

(notice the little dot at end) recursively formats, simplifies, and saves result into every file under current directory. I have a shell alias gf defined as gofmt -s -w . and find it quite handy.

Try gofmt -l . (list files whose formatting differs from gofmt's) first if you want 整个源代码树上运行Go fmt

答案3

得分: 50

此外,您可以尝试在您的项目目录中运行以下命令:

go fmt ./...
英文:

Also, you can try to run command:

go fmt ./...

from your project directory.

答案4

得分: 6

找到proj目录下的所有文件,并执行go fmt命令来格式化这些文件。

英文:
find proj -type f -iregex &#39;.*\.go&#39; -exec go fmt &#39;{}&#39; +

Explanation

  • find proj: find everything in this directory...
    • -type f: ...that is a file
    • -iregex &#39;.*\.go&#39;: ...and case-insensitively matches the regular expression .*\.go
  • ...and execute go fmt followed by as many matched files as the operating system can handle passing to an executable in one go.

答案5

得分: 5

如果您正在使用GoLand IDE,右键单击项目,您将找到Go Tools

英文:

if you are using GoLand IDE, right click on project and you will find Go Tools.
整个源代码树上运行Go fmt

答案6

得分: 3

The command gofmt ./... mentioned by some, does not work on Windows (at least on my Win7).

Instead of it, I used gofmt -d .\ which works recursively. I use the -d flag because I want to list the changes I need to make in order to pass the check.

NB: golint ./... does work on Windows, just gofmt ./... doesn't.

英文:

The command gofmt ./... mentioned by some, does not work on Windows (at least on my Win7).

Instead of it, I used gofmt -d .\ which works recursively. I use the -d flag because I want to list the changes I need to make in order to pass the check.

NB: golint ./... does work on Windows, just gofmt ./... doesn't.

huangapple
  • 本文由 发表于 2012年11月10日 12:11:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/13319165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定