现在有一组数据,如:
| 编号(id) | 名称(name) | 分组(group) |
| -------- | -------- |-------- |
|1|张三|A组|
|2|李四|A组|
|3|王麻子|B组|
查询出来后遍历,按分组赋值 (map[分组]=list)的形式
java的话 类似(queryList 为上面3条数据):
```go
Map map = new HashMap();
for (Map obj : queryList) {
List<Map> valList = (List<Map>) map.get(obj.get("group"));
if(valList == null) valList = new ArrayList<Map>();
valList.add(obj);
map.put(obj.get("group"), valList);
}
```
go的话:
valList, _ := map[val["group"].(string)]
这个 valList 怎么转型呢
len(valList) 直接报错了:invalid argument valList (type interface {}) for len
赋值也报错:invalid operation: valList[1] (type interface {} does not support indexing)
顺便请教下,不知道list数组([]map[string]interface{})具体的长度,需要怎么实例化呢?
先谢谢了
<code>
package main
import (
"container/list"
"fmt"
)
type listItem struct{
id int
name string
}
func main() {
m :=make(map[string]*list.List, 10)
l := list.New()
item := listItem{
id:1,
name:"abc",
}
l.PushBack(item)
m["key"] = l
for _, li := range m {
fmt.Println("len", li.Len())
for e := li.Front(); e != nil; e = e.Next() {
fmt.Print(e.Value) //输出list的值,01234
}
}
}
</code>
#1