为什么在这里需要分号?
Here is a test Go program:
package main
import fmt "fmt"
func main () {
ex := "moo cow
";
fmt.Print (ex)
}
With the semicolon, it compiles. However, if the semicolon is removed, it doesn't:
string.go:5: syntax error near fmt
Any ideas?
Update (March 2012): Newer Go releases are able to compile both forms (with and without the semicolon).
这是一个测试Go程序: p>
package main \ nimport fmt“ fmt”
func main(){
ex:=“ moo cow
”;
fmt.Print(ex)
}
code> pre>
使用分号进行编译。 但是,如果删除了分号,则不会: p>
string.go:5:fmt
pre>
附近的语法错误 ? p>
更新(2012年3月) strong>:较新的Go版本可以编译两种形式(带和不带分号)。 / p>
div>
From The Go Programming Language Specification:
Elements of a list of statements are separated by semicolons, which may be omitted only if the previous statement:
- ends with the closing parenthesis ")" of a list of declarations; or
- ends with a closing brace "}" that is not part of an expression.
Well, the answer's technically here: http://golang.org/doc/effective_go.html#semicolons, but it's not very clear. It looks like you can leave semicolons off the end of statements, but not expressions. I think (I'm not certain here) that expressions are anything with an "=" (or ":=") in them (although += seems to be fine in the tutorial?)