In the first case I pass a map to by value: package main
import (
"fmt"
"time"
)
func timeMap(z map[string]interface{}) {
z["updated_at"] = time.Now()
}
func main() {
foo := map[string]interface{}{
"Matt": 42,
}
timeMap(foo)
fmt.Println(foo)
}
The output is a muted map:
map[updated_at:2009-11-10 23:00:00 +0000 UTC Matt:42]
In the second case the code is almost identical but for passing by reference:
package main
import (
"fmt"
"time"
)
func timeMap(z *map[string]interface{}) {
(*z)["updated_at"] = time.Now()
}
func main() {
foo := map[string]interface{}{
"Matt": 42,
}
timeMap(&foo)
fmt.Println(foo)
}
Obviously, the result differs:
map[Matt:42 updated_at:2009-11-10 23:00:00 +0000 UTC]
My expectations were the following:
- when passing by value map is not muted
- When passing by reference map is muted like in the second case. However, in the first case map was muted but in the reverse order (compared to the second case).
Why does it happen so?