Guest User

Untitled

a guest
Mar 19th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. # Evaluation order of assignment operator in GoLang
  2.  
  3. In `GoLang`, the evaluation order of assignment operator is straight forward.
  4.  
  5. The order is:
  6. 1. Evaluate the `lvalues` to `Memory Location` from left to right.
  7. 2. Evaluate the `rvalues` to `constant` values from left to right.
  8. 3. And finallly, assign `rvalues` to `lvalues` from left to right.
  9.  
  10. The POC:
  11.  
  12. ```go
  13. package main
  14.  
  15. import (
  16. "fmt"
  17. )
  18.  
  19. func main() {
  20. arr := make([]int, 3)
  21.  
  22. arr[echo(0)], arr[echo(1)], arr[echo(2)] = echo(3), arr[echo(0)]+echo(4), echo(5)
  23. fmt.Println(arr)
  24. }
  25.  
  26. func echo(v int) int {
  27. fmt.Println(v)
  28. return v
  29. }
  30. ```
  31.  
  32. The ouput:
  33. ```txt
  34. 0
  35. 1
  36. 2
  37. 3
  38. 0
  39. 4
  40. 5
  41. [3 4 5]
  42. ```
  43.  
  44. ## Examples
  45.  
  46. swap.go
  47. ```go
  48. package main
  49.  
  50. import (
  51. "fmt"
  52. )
  53.  
  54. func main() {
  55. a, b := 1, 2
  56. swap(&a, &b)
  57. fmt.Println(a, b)
  58. }
  59.  
  60. func swap(a, b *int) {
  61. *a, *b = *b, *a
  62. }
  63. ```
Add Comment
Please, Sign In to add comment