From 075757669c2cab18c601dfe601a012ca066db11f Mon Sep 17 00:00:00 2001 From: nikkiing <1031497516@qq.com> Date: Thu, 16 Mar 2023 17:37:00 +0800 Subject: [PATCH] pkg --- pkg/tree.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/pkg/tree.go b/pkg/tree.go index 4b4f36d..8118d22 100644 --- a/pkg/tree.go +++ b/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 { path := pathFunc(v) if len(path) < length { - return nil + return } p := path[:len(path)-length] if p == parent { @@ -25,3 +25,17 @@ func Tree[T any](data []T, parent string, length int, pathFunc func(d T) string) } 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 +}