Golang用递归实现菜单树
package common
import "study/model"
type MenuTree struct {
*model.Menu
Children []MenuTree `json:"children"`
}
// BuildTree 构建菜单树
func BuildTree(allMenu []*model.Menu, pid int64) []MenuTree {
menuTree := make([]MenuTree, 0, len(allMenu))
for _, menu := range allMenu {
if pid != menu.ParentId {
continue
}
mt := MenuTree{
Menu: menu,
Children: BuildTree(allMenu, menu.ID),
}
menuTree = append(menuTree, mt)
}
return menuTree
}