Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post-increment operator in function argument in Go, not possible?

Tags:

go

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++)
    }
}
like image 445
conradkleinespel Avatar asked Apr 23 '14 22:04

conradkleinespel


1 Answers

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.

like image 87
andybalholm Avatar answered Oct 30 '22 02:10

andybalholm