Question

Here is a test Go program:

package main
import fmt "fmt"
func main () {
    ex := "moo cow\n";
    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).

Was it helpful?

Solution

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.

OTHER TIPS

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?)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top