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