树结构的菜单,golang 实现的打印效果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package main
import "fmt"
type Org struct {
OrgID string
OrgName string
parentID string
}
func printTree(tbl []Org, parent string, depth int) {
for _, r := range tbl {
if r.parentID == parent {
for i := 0; i < depth; i++ {
fmt.Print("--")
}
fmt.Print(r.OrgName, "\n\n")
printTree(tbl, r.OrgID, depth+1)
}
}
}
func main() {
data := []Org{
{"A001", "Dept", "0 -----th top"},
{"A002", "subDept1", "A001"},
{"A003", "sub_subDept", "A002"},
{"A006", "gran_subDept", "A003"},
{"A004", "subDept2", "A001"},
}
printTree(data, "0 -----th top", 0)
}
输出:
1
2
3
4
5
6
7
8
9
Dept
--subDept1
----sub_subDept
------gran_subDept
--subDept2
摘自 https://play.golang.org/p/27CQAhI8gf
本文网址: https://golangnote.com/topic/253.html 转摘请注明来源