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
- def upone(mutable, index):
- mutable[index] = mutable[index].upper()
- list = ['a', 'b', 'c']
- upone(list, 1)
- print list # ['a', 'B', 'c']
- dict = {'a': 'anders', 'b': 'bengt'}
- upone(dict, 'b')
- print dict # {'a': 'anders', 'b': 'BENGT'}
Go
- package main
- import (
- "fmt"
- "strings"
- )
- func upone_list(thing []string, index int) {
- thing[index] = strings.ToUpper(thing[index])
- }
- func upone_map(thing map[string]string, index string) {
- thing[index] = strings.ToUpper(thing[index])
- }
- func main() {
- // mutable
- list := []string{"a", "b", "c"}
- upone_list(list, 1)
- fmt.Println(list) // [a B c]
- // mutable
- dict := map[string]string{
- "a": "anders",
- "b": "bengt",
- }
- upone_map(dict, "b")
- fmt.Println(dict) // map[a:anders b:BENGT]
- }