Skip to content

Instantly share code, notes, and snippets.

@channyeintun
Last active November 10, 2024 20:26
package main
func changeMap(m1 *map[string]int) {
m1 = &map[string]int{"hello": 2}
}
func changeMap2(m2 *map[string]int) {
*m2 = map[string]int{"hello": 2} // without dereferencing, Go cannot achieve this
}
func changeMap3(m3 map[string]int) {
m3 = map[string]int{"hello": 2}
}
func main() {
m1 := map[string]int{"hello": 1}
changeMap(&m1)
println(m1["hello"]) // 1
m2 := map[string]int{"hello": 1}
changeMap2(&m2)
println(m2["hello"]) // 2
m3 := map[string]int{"hello": 1}
changeMap3(m3)
println(m3["hello"]) // 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment