Golang编码规范:何时在代码中换行?

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

Golang Coding Conventions: When to Break Lines in Code?

问题

我最喜欢 Go 语言的一件事情就是 gofmt。我喜欢这种编码风格在很多方面都是标准化的。

然而,有一件事情仍然让我困扰:换行。我发现,如果我看一下我公司的代码库,关于何时换行的问题并没有明显的一致性。

stdlib 中的一些代码为例,比如这里的 fmt 代码1,我仍然不确定是否存在惯例(如果有的话)。

例如,在这里的 fmt_integer 函数2中,if 语句体后面有换行,但在这里的 fmt_sbx 函数3中却没有。

在 Go 语言中是否有关于换行的惯例?什么被认为是最佳实践?

英文:

One of my favorite things about go is gofmt. I like the fact that the coding style is standardized in many ways.

However there is one thing that bothers me still: line breaks. I found that if I look at my company's codebase, there is no evident consistency on the question of when to break lines.

Looking at some of the code in the stdlib, for example, the fmt code here, I'm still not sure what's the convention (if it exists at all).

For example, on fmt_integer function here, there are line breaks after if statement bodies, but on fmt_sbx here there aren't.

Is there a convention for line breaks in golang? what's considered best practice?

答案1

得分: 3

首先,在 if 块之后最多只能有一行空行(因为 go fmt 的原因)。

其次,是否添加额外的空行没有严格的规定,只要保证代码可读性即可。

在《Go 代码审查评论》维基页面中甚至没有提到这一点。

英文:

First, there will be at most one empty line after an if block (because of go fmt).

Second, adding or not an extra blank line does not follow any hard rule, except code readability.

It is not even mentioned in the "Go Code Review Comments" wiki page.

huangapple
  • 本文由 发表于 2017年2月18日 22:02:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/42316112.html
匿名

发表评论

匿名网友

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

确定