In GoLang
, the evaluation order of assignment operator is straight forward.
The order is:
- Evaluate the
lvalues
toMemory Location
from left to right. - Evaluate the
rvalues
toconstant
values from left to right. - And finallly, assign
rvalues
tolvalues
from left to right.
The POC:
package main
import (
"fmt"
)
func main() {
arr := make([]int, 3)
arr[echo(0)], arr[echo(1)], arr[echo(2)] = echo(3), arr[echo(0)]+echo(4), echo(5)
fmt.Println(arr)
}
func echo(v int) int {
fmt.Println(v)
return v
}
The ouput:
0
1
2
3
0
4
5
[3 4 5]
swap.go
package main
import (
"fmt"
)
func main() {
a, b := 1, 2
swap(&a, &b)
fmt.Println(a, b)
}
func swap(a, b *int) {
*a, *b = *b, *a
}