我有一个关于 Go 的 gofmt 工具的问题,它根据官方 Go 规范自动格式化程序的输出(例如,你不能争论 Go 中括号应该放在哪里,因为那是显然是由规范修复的)。
在下一页:
http://golang.org/doc/effective_go.html
在“格式化”段落下,写着:
As an example, there's no need to spend time lining up the comments on the fields of a structure. Gofmt will do that for you. Given the declaration
type T struct {
name string // name of the object
value int // its value
}
gofmt will line up the columns:
type T struct {
name string // name of the object
value int // its value
}
但是我不明白这怎么可能与 diff 和 VCSes 一起玩得很好。
例如,如果我换行:
confuzzabler int // doo doo be doo
然后运行一个diff,我应该得到这个:
2d1
< confuzzabler int // doo doo be doo
7d5
<
生活会很美好:差异显示唯一发生变化的行。
但是,如果我重新运行 gofmt,我会得到这个:
type T struct {
confuzzabler int // doo doo be doo
name string // name of the object
value int // its value
}
现在我重新运行 diff,我得到了这个:
2,4c2,3
< confuzzabler int // doo doo be doo
< name string // name of the object
< value int // its value
---
> name string // name of the object
> value int // its value
7d5
<
这是一个非常困惑和误导的 diff 输出,因为只有一行发生了变化。
作为 Go 开发者,您如何处理这个问题?
最佳答案
$ diff --help|grep -i white
-b --ignore-space-change Ignore changes in the amount of white space.
-w --ignore-all-space Ignore all white space.
关于 VCS 的问题,如果您按照一些既定的约定自行格式化代码(假设这里的约定是 gofmt
遵循的),您将手动重新格式化该代码块中的空白与 gofmt
完全一样,并且此更改将被任何 VCS 计为更改。所以在这种情况下,我真的没有看到任何语义问题。如果您反而关心 VCS 提供的差异工具,您可能应该看看它们是否像上面提到的 GNU diff 那样支持忽略空白更改。 FWIW git diff
使用相同的 -b
命令行选项支持这一点。
关于version-control - Go 的 gofmt 和 diff/VCS 问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7348548/