version-control - Go 的 gofmt 和 diff/VCS 问题?

标签 version-control diff go

我有一个关于 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/

相关文章:

git - 使用 svn external 将 svn 迁移到 git

vim - 比较 Vim 中的两个选项卡

go - 如何检索选中的值

当它们应该是值时返回为 0 的 JSON 整数

go - 将 JSON 整数解码为空接口(interface)会导致错误的类型断言

PHP - 数据库模式 : version control, 分支、迁移

c# - 源代码管理中的存储过程 - 自动化构建/部署过程

reactjs - React 和 Preact diff 算法深度有什么区别

linux - 为什么 linux shell diff 命令有时会显示与空行相关的输出,即使存在 -B (--ignore-blank-lines) 选项?

git - 在 Git 中恢复移动文件的历史记录