Mutables

Python doesn't have the concept of pointers. Go does. But with Go youcan send an array or a map into a function, have it modified therewithout being returned and it gets changed.

Python

  1. def upone(mutable, index):
  2. mutable[index] = mutable[index].upper()
  3.  
  4.  
  5. list = ['a', 'b', 'c']
  6. upone(list, 1)
  7. print list # ['a', 'B', 'c']
  8.  
  9. dict = {'a': 'anders', 'b': 'bengt'}
  10. upone(dict, 'b')
  11. print dict # {'a': 'anders', 'b': 'BENGT'}

Go

  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "strings"
  6. )
  7.  
  8. func upone_list(thing []string, index int) {
  9. thing[index] = strings.ToUpper(thing[index])
  10. }
  11.  
  12. func upone_map(thing map[string]string, index string) {
  13. thing[index] = strings.ToUpper(thing[index])
  14. }
  15.  
  16. func main() {
  17. // mutable
  18. list := []string{"a", "b", "c"}
  19. upone_list(list, 1)
  20. fmt.Println(list) // [a B c]
  21.  
  22. // mutable
  23. dict := map[string]string{
  24. "a": "anders",
  25. "b": "bengt",
  26. }
  27. upone_map(dict, "b")
  28. fmt.Println(dict) // map[a:anders b:BENGT]
  29. }