type Student struct{
name string
}
func main(){
m := map[string]Student{"people", {"aaa"}}
m["people"] = "bbb"
}
报错:不能修改结构体中字典中的属性值
1 package main
2
3 import "fmt"
4
5 type Student struct{
6 name string
7 }
8
9 func main(){
10 k := map[string]int{}
11 k["ccc"] = 2
12 k["ddd"] = 3 //普通int类型可以直接修改
13 k["ddd"] = 4
14 fmt.Println(k)
15
16 m := map[string]Student{"people": {"aaa"}}
17 m["people"] = Student{"bb"} 不调用也可以
18 fmt.Println(m)
19 }
或者用深拷贝
package main
2
3 import "fmt"
4
5 type Student struct{
6 name string
7 }
8
9 func main(){
10 m := map[string]Student{"people": {"aaa"}}
11
12 s := m["people"] //深拷贝
13 s.name = "bb"
14 fmt.Println(s)
15 fmt.Println(m)
16 }
输出:
[root@localhost GoChatroom]# ./test
{bb}
map[people:{aaa}]