-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Closed
Labels
Description
A parsing ambiguity arises when a composite literal using the TypeName form of the LiteralType appears between the keyword and the opening brace of the block of an "if", "for", or "switch" statement, because the braces surrounding the expressions in the literal are confused with those introducing the block of statements. To resolve the ambiguity in this rare case, the composite literal must appear within parentheses. if x == (T{a,b,c}[i]) { ... } if (x == T{a,b,c}[i]) { ... } Composite literals, The Go Programming Language Specification. http://golang.org/doc/go_spec.html#Composite_literals The second example is invalid because it does not survive gofmt. package main type T [3]int func main() { var a, b, c, i, x int if (x == T{a, b, c}[i]) { } } After gofmt -w, package main type T [3]int func main() { var a, b, c, i, x int if x == T{a, b, c}[i] { } } 6g error: 7: syntax error: unexpected }, expecting := or = or comma hg id 4c76cc96e113 tip