Go 递归遍历树状图

380 阅读1分钟
package main
type SumIndustryLabel struct {
   Id                string      `json:"id" `
   Label             string      `json:"label" `
   Children             []SumIndustryLabel `json:"children"  `
}

func main() {
    var list []dto.SumIndustryLabel
    traversingSumIndustryLabel(list)
}

func traversingSumIndustryLabel(list []dto.SumIndustryLabel) {
   for item := range list{
       // 直接使用list会改变原list从而导致数组越界异常
      var temp []dto.SumIndustryLabel
      if len(list[item].Children) <= 0 {
         fmt.Println(list[item].Label)
      }else {
         fmt.Println(list[item].Label)
         temp = list[item].Children
         e.traversingSumIndustryLabel(temp)
      }
   }
}