Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Evaluation order of assignment operator in GoLang
- In `GoLang`, the evaluation order of assignment operator is straight forward.
- The order is:
- 1. Evaluate the `lvalues` to `Memory Location` from left to right.
- 2. Evaluate the `rvalues` to `constant` values from left to right.
- 3. And finallly, assign `rvalues` to `lvalues` from left to right.
- The POC:
- ```go
- 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:
- ```txt
- 0
- 1
- 2
- 3
- 0
- 4
- 5
- [3 4 5]
- ```
- ## Examples
- swap.go
- ```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
- }
- ```
Add Comment
Please, Sign In to add comment