nikkiing 2 years ago
parent 4bd2709c04
commit 075757669c
  1. 16
      pkg/tree.go

@ -13,7 +13,7 @@ func Tree[T any](data []T, parent string, length int, pathFunc func(d T) string)
for _, v := range data { for _, v := range data {
path := pathFunc(v) path := pathFunc(v)
if len(path) < length { if len(path) < length {
return nil return
} }
p := path[:len(path)-length] p := path[:len(path)-length]
if p == parent { if p == parent {
@ -25,3 +25,17 @@ func Tree[T any](data []T, parent string, length int, pathFunc func(d T) string)
} }
return return
} }
// 拆分层级结构 A00001B00001C00001 拆分为 ["A00001","A00001B00001","A00001B00001C00001"]
// length A00001 一个节点长度
func Split(code string, length int) (res []string) {
if len(code) < length {
return
}
times := len(code) / length
for i := 0; i < times; i++ {
res = append(res, code[:len(code)-length*i])
}
return
}

Loading…
Cancel
Save