Last active
November 10, 2024 20:26
-
-
Save channyeintun/435a11ef726d344d25ca06dd2de8f1fc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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