树形结构的实现

package main

import (
   "fmt"
   "strings"
)

// 地区结构体
type region struct {
   name     string   // 地区名称
   postcode string   // 邮政编码
   sub      []region // 下属地区【重点:字段的数据类型为region结构体切片】
}

// 地区数据
var regionData = []region{
   {
      name:     "湖北省",
      postcode: "",
      sub:      []region{},
   },
   {
      name:     "湖南省",
      postcode: "",
      sub:      []region{},
   },
   {
      name:     "广东省",
      postcode: "",
      sub: []region{
         {
            name:     "广州市",
            postcode: "",
            sub: []region{
               {
                  name:     "天河区",
                  postcode: "510630",
                  sub:      []region{},
               },
               {
                  name:     "黄埔区",
                  postcode: "510700",
                  sub:      []region{},
               },
               {
                  name:     "越秀区",
                  postcode: "510030",
                  sub:      []region{},
               },
            },
         },
         {
            name:     "深圳市",
            postcode: "",
            sub: []region{
               {
                  name:     "南山区",
                  postcode: "518000",
                  sub:      []region{},
               },
               {
                  name:     "福田区",
                  postcode: "518000",
                  sub:      []region{},
               },
               {
                  name:     "龙岗区",
                  postcode: "518100",
                  sub:      []region{},
               },
            },
         },
         {
            name:     "佛山市",
            postcode: "",
            sub: []region{
               {
                  name:     "顺德区",
                  postcode: "528300",
                  sub:      []region{},
               },
               {
                  name:     "南海区",
                  postcode: "528200",
                  sub:      []region{},
               },
               {
                  name:     "禅城区",
                  postcode: "528000",
                  sub:      []region{},
               },
            },
         },
      },
   },
   {
      name:     "广西省",
      postcode: "",
      sub:      []region{},
   },
   {
      name:     "江西省",
      postcode: "",
      sub:      []region{},
   },
}

// GetRegionTree 获取地区(树形结构)
func GetRegionTree(data []region, level int64) string {
   if data == nil {
      data = regionData
   }

   prefix := strings.Repeat("|--", int(level))

   tree := ""

   for _, node := range data {
      postcode := node.postcode
      if postcode != "" {
         postcode = "(" + postcode + ")"
      }

      tree += prefix + node.name + postcode + "\n"

      // 如果有下属地区就递归处理
      if len(node.sub) > 0 {
         tree += GetRegionTree(node.sub, level+1)
      }
   }

   return tree
}

func main() {
   regionTree := GetRegionTree(nil, 0)
   fmt.Print(regionTree)
}

/********** 输出结果·开始 **********
湖北省
湖南省
广东省
|--广州市
|--|--天河区(510630)
|--|--黄埔区(510700)
|--|--越秀区(510030)
|--深圳市
|--|--南山区(518000)
|--|--福田区(518000)
|--|--龙岗区(518100)
|--佛山市
|--|--顺德区(528300)
|--|--南海区(528200)
|--|--禅城区(528000)
广西省
江西省
********** 输出结果·结束 **********/

Copyright © 2024 码农人生. All Rights Reserved