How come, in Go (1.2.1), this works?
package main
import (
"fmt"
)
func main() {
var i = 0
for i < 10 {
fmt.Println(i)
i++
}
}
But this (with the increment operator in the function argument) doesn't?
package main
import (
"fmt"
)
func main() {
var i = 0
for i < 10 {
fmt.Println(i++)
}
}
In Go, i++
is a statement, not an expression. So you can't use its value in another expression such as a function call.
This eliminates the distinction between post-increment and pre-increment, which is a source of confusion and bugs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With