前段时间写了个递归树,中间遇到了个坑,按逻辑看是没问题,没想到结果不对。
for _, v := range tree
这应该是golang和java思想的不同之处,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
35
36
37
38
func genGeoTree(arr *[]entity.GeoCommon) []*entity.GeoTree {
root := buildGeoRoot(arr)
buildGeoChildren(arr, root)
return root
}
func buildGeoRoot(arr *[]entity.GeoCommon) []*entity.GeoTree {
var pNodes []*entity.GeoTree
for _, v := range *arr {
if v.PCode == 0 {
var child entity.GeoTree
child.Code = v.Code
child.Name = v.Name
child.Sort = v.Sort
child.PCode = v.PCode
pNodes = append(pNodes, &child)
}
}
return pNodes
}
func buildGeoChildren(arr *[]entity.GeoCommon, tree []*entity.GeoTree) {
for _, v := range tree {
pNodes := make([]*entity.GeoTree, 0)
for _, vv := range *arr {
if vv.PCode == v.Code {
var child entity.GeoTree
child.Code = vv.Code
child.Name = vv.Name
child.Sort = vv.Sort
child.PCode = vv.PCode
pNodes = append(pNodes, &child)
buildGeoChildren(arr, pNodes)
}
}
v.Children = pNodes
}
}