Why doesn’t these lines compile?
i := 0 fmt.Println(++i) fmt.Println(i++)
main.go:9:14: syntax error: unexpected ++, expecting expression main.go:10:15: syntax error: unexpected ++, expecting comma or )
In Go increment and decrement operations can’t be used as expressions, only as statements. Also, only the postfix notation is allowed.
The above snippet needs to be written as:
i := 0 i++ fmt.Println(i) fmt.Println(i) i++
Without pointer arithmetic, the convenience value of pre- and postfix increment operators drops. By removing them from the expression hierarchy altogether, expression syntax is simplified and the messy issues around order of evaluation of
p[i] = q[++i]) are eliminated as well. The simplification is significant. Go FAQ: Why are ++ and – statements and not expressions?
Share this page: