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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main

import "fmt"

type Info struct {
ID string
Name string
ParentId string
}

func main() {

var data []Info

data = append(data,
Info{"7", "测试7", "2"},
Info{"2", "测试2", "1"},
Info{"1", "测试1", ""},
Info{"8", "测试-8", "7"},
Info{"9", "测试-9", "8"},
Info{"10", "测试-10", "9"},
Info{"101", "测试-101", "9"},
Info{"102", "测试-102", "9"},
Info{"1001", "测试-1001", "101"},


Info{"122", "节点2", "20000"},
Info{"123", "节点3", "122"},
Info{"124", "节点4", "123"},
Info{"127", "节点7", "126"},
Info{"125", "节点5", "124"},
Info{"126", "节点6", "125"},
Info{"128", "节点8", "127"},
Info{"20000", "顶级节点", ""},

Info{"a", "宋江", ""},
Info{"b", "卢俊义", "a"},
Info{"c", "无用", "b"},


)

fmt.Println(data)

// 每一条的id均不相同, 顶级节点的parent_id为空,能保证每个都能连成一棵树
// 输出当id=7时,该节点以上及以下的id值

rootSli := []Info{}

for _, v := range data {

if v.ParentId == "" {
fmt.Println(v.ID)
rootSli = append(rootSli, v)

}
}

}