看了针对下面的结构体,map 定义为 : likes := make(map[string] *Person) 那怎么给likes 添加元素呢。
尝试了诸如 likes["a"].Likes = append(likes["a"], code) 或者 likes["a"].Likes = append(likes["a"].Likes, code) 都不可以。 其中code 是一个 string类型的标亮
```go
type Person struct {
Likes []string
}
```
likes := make(map[string]*Person)
a := &Person{}
likes["a"] = a
if likes["a"].Likes == nil {
likes["a"].Likes = make([]string, 0)
}
likes["a"].Likes = append(likes["a"].Likes, "test01")
likes["a"].Likes = append(likes["a"].Likes, "test02")
#6
更多评论
type Person struct {
Likes []string
}
p := new(Person)
p.Likes = append(p.Likes, "movie")
logger.Debug("%v", p.Likes)
result: [movie]
#1
type Person struct {
Likes []string
}
p := Person{make([]string, 1)}
p.Likes = append(p.Likes, "movie")
logger.Debug("%v", p.Likes)
p = Person{Likes: make([]string, 1)}
p.Likes = append(p.Likes, "movie")
logger.Debug("%v", p.Likes)
#2