golang生成树状菜单
参考:https://stackoverflow.com/questions/59709917/build-a-tree-structure-from-parent-child-pairs-in-go
Firstly, you should never use pointers to slices unless you really have to. It's more typical to assign a return value to the variable, e.g. mySlice = sliceReturningFunction()
.
I'm not sure what all the requirements here are, but one solution could be:
- Build a map of parent-child relations (
map[int][]int
). - Pass the root-level relation to a function that recursively builds the categories.
Here's an example recursive function. Note that it returns a new slice rather than mutating a pointer.
func buildCategories(ids []int, relations map[int][]int) []Category {
categories := make([]Category, len(ids))
for i, id := range ids {
c := Category{ID: id}
if childIDs, ok := relations[id]; ok {
c.Child = buildCategories(childIDs, relations)
}
categories[i] = c
}
return categories
}
I added a full example on the Playground. It's not tested, and I'm sure there are better solutions, but it's simple and might give you some ideas at least. If you're going to have thousands of nodes and this is accessed frequently, you're going to need to optimise beyond Go code alone though.
-------------------------------------------------------------------
下面也是一种方法
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 | package main import ( "encoding/json" "fmt" ) type Category struct { ID int `json: "id" ` ParentId int `json: "parent_id" ` Name string `json: "name" ` Url string `json: "url" ` Method string `json: "method" ` Child []Category `json: "child,omitempty" ` } func main() { dataset := [][]int{{1, 0}, {10, 0}, {2, 1}, {3, 1}, {4, 0}, {5, 4}, {6, 4}, {7, 0}, {8, 7}, {9, 2}} // dataset := []Category{{1, 0, "aa", "/ad", "GEt", nil}, {10, 0, "aa", "/ad", "GEt", nil}, {2, 1, "aa", "/ad", "GEt", nil}, {3, 1, "aa", "/ad", "GEt", nil}, // {4, 0, "aa", "/ad", "GEt", nil}, {5, 4, "aa", "/ad", "GEt", nil}, {6, 4, "aa", "/ad", "GEt", nil}, {7, 0, "aa", "/ad", "GEt", nil}, {8, 7, "aa", "/ad", "GEt", nil}, // {9, 2, "aa", "/ad", "GEt", nil}} relations := relationMap(dataset) categories := buildCategories(relations[0], relations) // pass root-level IDs j, _ := json.Marshal(categories) fmt.Println(string(j)) } func relationMap(dataset [][]int) map [int][]int { relations := make( map [int][]int) for _, relation := range dataset { child, parent := relation[0], relation[1] relations[parent] = append(relations[parent], child) } return relations } func buildCategories(ids []int, relations map [int][]int) []Category { categories := make([]Category, len(ids)) for i, id := range ids { c := Category{ID: id} if childIDs, ok := relations[id]; ok { // build child's children c.Child = buildCategories(childIDs, relations) } categories[i] = c } return categories } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
2020-08-17 【转】angular @HostListener()
2018-08-17 apt安装遇到的问题
2017-08-17 With Storm Spouts, when is declareOutputFields( ) called?
2017-08-17 How to run Java main class and pass application arguments in Maven?