1
btjoker 2018-03-12 22:07:03 +08:00
因为你取到的是复制品啊
m := map[string]data { "x": data {"one"} } n := data {"two"} m[x] = n |
2
btjoker 2018-03-12 22:10:01 +08:00
不好意思, 引号和逗号掉了
雨痕的 Go 学习笔记, 建议看看 |
3
fuxiaohei 2018-03-12 22:12:45 +08:00 1
https://golang.org/ref/spec#Assignments
Each left-hand side operand must be addressable, a map index expression, or (for = assignments only) the blank identifier. Operands may be parenthesized. https://golang.org/ref/spec#Address_operators For an operand x of type T, the address operation &x generates a pointer of type *T to x. The operand must be addressable, that is, either a variable, pointer indirection, or slice indexing operation; or a field selector of an addressable struct operand; or an array indexing operation of an addressable array. As an exception to the addressability requirement, x may also be a (possibly parenthesized) composite literal. If the evaluation of x would cause a run-time panic, then the evaluation of &x does too. 赋值的对象必须是 addressable,用 *T 吧 |
4
laike9m 2018-03-12 22:21:11 +08:00
|
5
admirez OP 嗯,用*T 搞定了
|
6
ox180 2018-03-13 12:53:22 +08:00
```
func main(){ type data struct { name string } s := make(map[int]*data) s[1] = &data{name: "aa"} s[1].name = "bb" fmt.Println(*s[1]) } ``` |