`Laravel`版`小丑路人社区`改版中,与`Hyperf版小丑路人社区`数据互动,此版本改版中……尚未彻底完结!

Q:

golang Tree 树形结构

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)
Go
订阅

评论记录


评论/回复