在MS-Windows上使用vim和git处理go语言源文件时的行尾符问题。

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

Line-endings in go-language source files using vim and git on MS-Windows

问题

情况说明

我正在Windows 10上编写Go语言应用程序。我使用vim编辑我的源文件,并使用git进行版本控制。

Go语言对源文件格式有一些异常严格的规定。我决定如果我按照这些规定进行编码,我的生活会更轻松。

问题所在

go fmt命令对于排序导入、对齐列和其他操作非常有用。我倾向于在提交代码之前和其他时候使用它。

go fmt命令会将行尾换行符更改为lf。这会导致gitvim都发出警告。

我的解决方案?

在3个月后转移到“答案”部分,因为没有其他答案出现,对于其他遇到类似问题的人来说,看到这个问题有1个答案而不是0个答案可能更好

我的问题。

我的行尾解决方案是否最优,或者我是否忽略了可能会在以后困扰我的问题?

英文:

The situation

I am writing go language applications on Windows 10. I use vim to edit my source files. I use git for version control.

The go language comes with some unusually rigid prescriptions for source file formatting. I have decided my life will be easier if I go along with this.

The problem

the go fmt command is useful for sorting imports, lining up columns and other things. I'm inclined to use it prior to checkin and at other times.

The go fmt command changes line endings to lf. This causes both git and vim to issue warnings.

My solution?

Moved into an "Answer" after 3 months because no other answers appeared and it's probably better for other people with a similar problem to see in a search result that this question has 1 Answer rather than 0 Answers

My question.

Is my line-ending solution optimal or have I missed something that may bite me later?

答案1

得分: 0

我的解决方案。

为了消除警告,我配置了vim和git以符合golang的要求。

git

以下命令可以阻止git尝试执行通常正确的操作:在存储库中使用标准换行符,每个开发人员的工作目录中使用平台特定的换行符,根据需要进行转换。

git config core.autocrlf false

现在,git不会在检出时将lf更改为crlf,也不会对换行符发出警告。

vim

_vimrc中添加以下内容:

au FileType go setl ts=3 sw=3 nowrap nu syntax=go ruler fileformat=unix

fileformat=unix似乎可以让vim在处理非本地平台的换行符时不发出警告。


脚注

在发布上述问题的3个月后,我没有遇到任何缺点或问题-至少在我使用govimgit的方式上没有。

英文:

My solution.

To eliminate the warnings I configured vim and git to work the way golang likes.

git

The following command stops git from trying to do what is normally the right thing: standard line-endings in repo, platform line-endings on each developers working directory, convert as needed.

git config core.autocrlf false

Now git won't change lf to crlf on checkout or bleat about line-endings.

vim

In _vimrc

au FileType go setl ts=3 sw=3 nowrap nu syntax=go ruler fileformat=unix

The fileformat=unix seems to keep vim complaint-free regarding line-endings that are not native to the platform.


Footnote

3 months after posting the above question I haven't come across any drawbacks or problems - at least not the way I use go, vim and git.

huangapple
  • 本文由 发表于 2017年5月18日 17:58:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/44044556.html
匿名

发表评论

匿名网友

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

确定