type Menu struct {
Id int `json:"id"`
ParentId int `json:"parent_id"`
Name string `json:"name"`
Sort int `json:"sort"`
Child []*Menu `json:"child"`
}
func getTreeByParentId(list []*Menu, parent_id int) []*Menu {
menus := make([]*Menu, 0)
for _, menu := range list {
if menu.ParentId == parent_id {
menu.Child = getTreeByParentId(list, menu.Id)
menus = append(menus, menu)
}
}
return menus
}
// 获取Tree结构
menus := getTreeByParentId(list, 0)
bytes, _ := json.MarshalIndent(menus, "", " ")
fmt.Printf("%s\n", bytes)
评论/回复