https://my.oschina.net/chai2010/blog/161384
slice/map/chan这三种数据类型在函数中就是传址
从C转GO的我对这段代码会有点疑惑,ChangeInt函数不能修改tmpint的值,这个好理解,因为input只是tmpint的一个拷贝,但是ChangeMap却能修改tmpmap的值,照理说如果input也是tmpmap的拷贝,那么ChangeMap将不能修改tmpmap的值,上述的链接文章里可以看到map数据类型其实是传指针(或者说传地址)。因此map内的数据可以在ChangeMap函数中被修改(这三种数据类型的隐含属性)
func ChangeInt(input int) {
input = 4
}
func ChangeMap(input map[int]string) {
input[2] = "changed"
}
func main() {
tmpmap := make(map[int]string)
tmpmap[1] = "myname"
tmpmap[2] = "yourname"
ChangeMap(tmpmap)
var tmpint int = 2
ChangeInt(tmpint)
fmt.Println(tmpmap,tmpint)
}