众所周知,golang中map是一个指针,既然是一个指针,那么参数传递、赋值应该都是指针传递,而下面的例子也印证了我的想法
package mainimport "fmt"func test_map2(m map[string]string) {fmt.Printf("inner: %v, %p, %p\n", m, m, &m)m = make(map[string]string, 0)m["a"] = "11"fmt.Printf("inner: %v, %p, %p\n", m, m, &m)
}func main() {// var m map[string]string //未初始化m := make(map[string]string)fmt.Printf("outer: %v, %p, %p\n", m, m, &m)test_map2(m)fmt.Printf("outer: %v, %p, %p\n", m, m, &m)n := mfmt.Printf("outer: %v, %p, %p\n", n, n, &n)
}
输出
outer: map[], 0xc0000160f0, 0xc000042020
inner: map[], 0xc0000160f0, 0xc000042030
inner: map[a:11], 0xc000016180, 0xc000042030
outer: map[], 0xc0000160f0, 0xc000042020
outer: map[], 0xc0000160f0, 0xc000042038