为什么我不能把左大括号放在下一行?

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

Why can't I put the opening braces on the next line?

问题

遇到了一个奇怪的错误,当我尝试编译以下代码时:
<pre>

package main

import fmt &quot;fmt&quot;

func main()
{
   var arr [3]int
   for i:=0; i&amp;lt;3; i++
   {
     fmt.Printf(&quot;%d&quot;,arr[i])
   }
}

</pre>
错误如下:
<pre>

unexpected semicolon or newline before {

</pre>

在更正后,以下代码可以正常工作:
<pre>

package main

import fmt &quot;fmt&quot;

func main(){
   var arr [3]int
   for i:=0; i&amp;lt;3; i++{
     fmt.Printf(&quot;%d&quot;,arr[i])
   }
}

</pre>

GO语言是如此严格类型化的吗?而且它也没有警告。难道这不应该是程序员自己决定如何格式化代码的选择吗?
<a href="https://stackoverflow.com/questions/7059616/go-language-warnings-and-errors">Go语言警告和错误</a>

英文:

Encountered a strange error when I tried to compile following code:
<pre>

package main

import fmt &quot;fmt&quot;

func main()
{
   var arr [3]int
   for i:=0; i&amp;lt;3; i++
   {
     fmt.Printf(&quot;%d&quot;,arr[i])
   }
}

</pre>
Error is as follows:
<pre>

unexpected semicolon or newline before {

</pre>

After correction following code worked:
<pre>

package main

import fmt &quot;fmt&quot;

func main(){
   var arr [3]int
   for i:=0; i&amp;lt;3; i++{
     fmt.Printf(&quot;%d&quot;,arr[i])
   }
}

</pre>

Is GO language this much strictly Typed? And this doesn't have warnings also. Should this not be a programmers choice how he wants to format his code?
<a href="https://stackoverflow.com/questions/7059616/go-language-warnings-and-errors">Go language warnings and errors</a>

答案1

得分: 16

Go语言会自动插入分号,因此{的唯一允许位置是在前一行的末尾。始终使用与gofmt生成的相同风格编写Go代码,就不会有问题。

请参阅Go的常见问题解答:为什么有大括号但没有分号?为什么不能将左大括号放在下一行?

英文:

The Go language does automatic semicolon insertion, and thus the only allowed place for { is at the end of the preceding line. Always write Go code using the same style as gofmt produces and you will have no problems.

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

答案2

得分: 9

go语言在特定规则下包含分号,对于你的情况,i++后的换行引入了一个分号,然后是'{'。请参考http://golang.org/doc/go_spec.html。

格式化在某种程度上是语言的一部分,使用gofmt可以使代码看起来相似,然而,你可以以许多不同的方式格式化你的代码。

英文:

go language includes semicolons with a specific rule, in your case, the newline after the i++ introduces a semicolon before the '{'. see http://golang.org/doc/go_spec.html.

formatting is somewhat part of the language, use gofmt to make code look similar, however, you can format your code many different ways.

答案3

得分: 5

Go代码有一个必需的括号风格。
就像程序员不能选择在Python中使用大括号,而是必须使用缩进一样。

这种必需的括号风格允许分号插入工作,而无需解析器预先查看下一行(如果您想实现GO代码的REPL,则非常有用)。

package main

func main();

是有效的Go代码,而解析器在不查看下一行的情况下假设这是您的意思,然后对您在其后放置的任何未连接到任何东西的块感到困惑。

在所有Go代码中使用相同的括号风格使阅读变得更容易,也避免了关于括号风格的讨论。

英文:

Go code has a required bracing style.
In the same way that a programmer can't choose to use braces in python and is required to use indentation.

The required bracing style allows the semicolon insertion to work without requiring the parser to look ahead to the next line(which is useful if you want to implement a REPL for GO code)

package main

func main();

is valid Go code and without looking at the next line the parser assumes this is what you meant and is then confused by the block that isn't connected to anything that you've put after it.

Having the same bracing style through all Go code makes it a lot easier to read and also avoids discussion about bracing style.

答案4

得分: 4

也许吧。我认为Go语言很好地避免了一些无休止的代码风格讨论,这是很好的。甚至有一个工具,gofmt,可以按照标准风格格式化代码,确保大多数Go代码遵循相同的指导方针。就像他们在说:“一致性比个人偏好更重要。习惯它,这是好的(tm)。”
1: http://golang.org/cmd/gofmt/

英文:

> Should this not be a programmers choice how he wants to format his
> code?

Maybe. I think it is nice that Go steps forward to avoid some bike-shedding, like never ending style discussions. There is even a tool, gofmt, that formats code in a standard style, ensuring that most Go code follows the same guidelines. It is like they were saying: "Consistency everywhere > personal preferences. Get used to it, This Is Good(tm)."

答案5

得分: 1

Go语言遵循严格的规则,以保持读者的独特可见性,就像Python一样,使用Visual Code IDE,它会进行自动格式化和错误检测。

英文:

Go lang fallows strict rules to maintain the unique visibility for the reader like Python, use visual code IDE, it will do automatic formatting and error detection.

huangapple
  • 本文由 发表于 2011年8月15日 14:39:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/7062276.html
匿名

发表评论

匿名网友

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

确定