coding-style - 为什么 Golang 强制大括号不在下一行?

标签 coding-style go curly-braces

正确:

if(true) {

}

不正确:

if(true)
{

}

为什么要强制执行这种风格,是否与语言规范有关,或者仅仅是因为他们更喜欢一种风格而不是另一种风格?

最佳答案

Why are there braces but no semicolons? And why can't I put the opening brace on the next line?

Go uses brace brackets for statement grouping, a syntax familiar to programmers who have worked with any language in the C family. Semicolons, however, are for parsers, not for people, and we wanted to eliminate them as much as possible. To achieve this goal, Go borrows a trick from BCPL: the semicolons that separate statements are in the formal grammar but are injected automatically, without lookahead, by the lexer at the end of any line that could be the end of a statement. This works very well in practice but has the effect that it forces a brace style. For instance, the opening brace of a function cannot appear on a line by itself.

http://golang.org/doc/faq#semicolons

关于coding-style - 为什么 Golang 强制大括号不在下一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17153838/

相关文章:

c++ - 将 vera++(或其他编码约定工具)与 Qt/QtCreator 集成

jQuery移动横向和纵向类

coding-style - 如何为不同的 clang-format 版本维护 .clang-format 文件?

java - java中变量或类名前的_(下划线)是否有标准?

go - 如何将 .go 文件编译到特定目录,或 gitignore 二进制文件?

go - 将 .go-file 作为附加参数传递给 `go run`

database - 如何从 interface{} 值(反射)为显式类型的结构成员设置新值?戈朗

curly-braces - 如何阻止 Sublime Text 3 在花括号中换行

url - Golang 中包含大括号的错误请求 400

java - 我应该在哪里放置牙套?