求大佬解答下为啥会报错,为什么不让修改呢?
type Student struct{
name string
}
func main(){
m := map[string]Student{"people", {"aaa"}}
m["people"] = "bbb"
}
//报错:不能修改字典中结构体属性的值
1
useben 2020-03-21 22:08:02 +08:00
type Student struct {
name string } func main() { m := map[string]Student{"people": {name: "ccc"}} m["people"] = Student{name: "bbb"} } |
2
none 2020-03-21 22:26:32 +08:00
你这代码是要把字符串赋值给结构体类型,相当于 var a Student = "bbb" 当然会报错了
|
4
input2output 2020-03-21 22:55:46 +08:00
要 Student 类型,你赋值的是 string 类型
|
5
lasuar 2020-03-21 23:45:59 +08:00
struct 建议存 pointer,方便直接通过 map[x]修改
|
6
xmge OP |