Evaluation order of function parameter In Go
According to the Go programming langauge specification [1], Go has certain rules when evaluating a statement/expression. Here is the detailed description of the evaluation order.
At package level, initialization dependencies determine the evaluation order of individual initialization expressions in variable declarations. Otherwise, when evaluating the operands of an expression, assignment, or return statement, all function calls, method calls, and communication operations are evaluated in lexical left-to-right order.
All function calls will use left-to-right order. Here is an example I wrote to verify this rule.
// go run left-to-right.go
package main
import "fmt"
func arg1() int{
fmt.Println("arg1 gets called");
return 1;
}
func arg2() int{
fmt.Println("arg2 gets called");
return 2;
}
func arg3() int{
fmt.Println("arg3 gets called");
return 3
}
func foo(a int, b int, c int){
return;
}
func main(){
foo(arg1(), arg2(), arg3());
}
As expected, the output is
arg1 gets called
arg2 gets called
arg3 gets called