dphnn333971
2017-02-18 14:02
浏览 523
已采纳

Golang编码约定:何时在代码中换行?

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?

图片转代码服务由CSDN问答提供 功能建议

我最喜欢的go语言之一是 gofmt 。 我喜欢这样的事实,编码风格在许多方面都是标准化的。

但是,有一件事仍然困扰着我:换行。 我发现,如果查看我公司的代码库,何时中断行的问题就没有明显的一致性。

查看 stdlib 中的某些代码,例如 fmt 代码此处,我仍然不确定约定(如果有的话)。

例如,在 fmt_integer 函数上此处,在 if 语句主体之后有换行符,但在 fmt_sbx 此处没有。

在golang中有换行约定吗? 什么是最佳做法?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doulan3436 2017-02-18 14:24
    已采纳

    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.

    点赞 打赏 评论

相关推荐 更多相似问题