package main type ArticleClass struct { Id int `json:"id"` ParentID int `json:"parent_id"` Name string `json:"name"` List []*ArticleClass `json:"list,omitempty"` } func main(){ var list []*ArticleClass // 假设已经赋值进去,从 数据库取出的数据 data := buildData(list) result := makeTreeCore(0, data) } func buildData(list []*ArticleClass) map[int]map[int]*ArticleClass { var data map[int]map[int]*ArticleClass = make(map[int]map[int]*ArticleClass) for _, v := range list { id := v.Id fid := v.ParentID if _, ok := data[fid]; !ok { data[fid] = make(map[int]*ArticleClass) } data[fid][id] = v } return data } func makeTreeCore(index int, data map[int]map[int]*ArticleClass) []*ArticleClass { tmp := make([]*ArticleClass, 0) for id, item := range data[index] { if data[id] != nil { item.List = makeTreeCore(id, data) } tmp = append(tmp, item) } return tmp }
golang 递归 无限级菜单分类
未经允许不得转载:云服务器代理商-凯铧互联 » golang 递归 无限级菜单分类
评论前必须登录!
注册