当前位置:网站首页>Go recursive infinite classification
Go recursive infinite classification
2022-06-12 03:02:00 【Give me a bottle of ice cream】
Recursive infinite classification
package main
import (
"encoding/json"
"io/ioutil"
"log"
)
type list struct {
ID int
Name string
TopObj *list
Pid int
Child []list
}
func handel(tree *list, v list){
if v.Pid == 0 {
v.TopObj = &list{
Name : tree.Name,
}
tree.Child = append(tree.Child, v)
}else{
if len(tree.Child) > 0 {
for key, value := range tree.Child {
if value.ID == v.Pid {
v.TopObj = &list{
Name: value.Name,
}
tree.Child[key].Child = append(tree.Child[key].Child, v)
}else{
handel(&tree.Child[key], v)
}
}
}
}
}
func main() {
a := []list{
{
ID:1,
Name:"A",
Pid:0,
},
{
ID:10,
Name:"Q",
Pid:0,
},
{
ID:2,
Name:"b",
Pid:1,
},
{
ID:3,
Name:"c",
Pid:0,
},
{
ID:4,
Name:"d",
Pid:3,
},
{
ID:11,
Name:"aa",
Pid:0,
},
{
ID:5,
Name:"E",
Pid:4,
},
{
ID:12,
Name:"BB",
Pid:11,
},
{
ID:13,
Name:"CC",
Pid:12,
},
{
ID:14,
Name:"DD",
Pid:13,
},
}
root := &list{
ID: 0,
Name: "/",
}
for _,v := range a {
handel(root, v)
}
jsonStr, err :=json.MarshalIndent(root," ","\t")
if err != nil {
log.Fatalln(err)
}
if err := ioutil.WriteFile("./tree.json", jsonStr, 0644); err != nil {
log.Fatalln(err)
}
}Generate a tree view
tree.json
{
"ID": 0,
"Name": "/",
"TopObj": null,
"Pid": 0,
"Child": [
{
"ID": 1,
"Name": "A",
"TopObj": {
"ID": 0,
"Name": "/",
"TopObj": null,
"Pid": 0,
"Child": null
},
"Pid": 0,
"Child": [
{
"ID": 2,
"Name": "b",
"TopObj": {
"ID": 0,
"Name": "A",
"TopObj": null,
"Pid": 0,
"Child": null
},
"Pid": 1,
"Child": null
}
]
},
{
"ID": 10,
"Name": "Q",
"TopObj": {
"ID": 0,
"Name": "/",
"TopObj": null,
"Pid": 0,
"Child": null
},
"Pid": 0,
"Child": null
},
{
"ID": 3,
"Name": "c",
"TopObj": {
"ID": 0,
"Name": "/",
"TopObj": null,
"Pid": 0,
"Child": null
},
"Pid": 0,
"Child": [
{
"ID": 4,
"Name": "d",
"TopObj": {
"ID": 0,
"Name": "c",
"TopObj": null,
"Pid": 0,
"Child": null
},
"Pid": 3,
"Child": [
{
"ID": 5,
"Name": "E",
"TopObj": {
"ID": 0,
"Name": "d",
"TopObj": null,
"Pid": 0,
"Child": null
},
"Pid": 4,
"Child": null
}
]
}
]
},
{
"ID": 11,
"Name": "aa",
"TopObj": {
"ID": 0,
"Name": "/",
"TopObj": null,
"Pid": 0,
"Child": null
},
"Pid": 0,
"Child": [
{
"ID": 12,
"Name": "BB",
"TopObj": {
"ID": 0,
"Name": "aa",
"TopObj": null,
"Pid": 0,
"Child": null
},
"Pid": 11,
"Child": [
{
"ID": 13,
"Name": "CC",
"TopObj": {
"ID": 0,
"Name": "BB",
"TopObj": null,
"Pid": 0,
"Child": null
},
"Pid": 12,
"Child": [
{
"ID": 14,
"Name": "DD",
"TopObj": {
"ID": 0,
"Name": "CC",
"TopObj": null,
"Pid": 0,
"Child": null
},
"Pid": 13,
"Child": null
}
]
}
]
}
]
}
]
}边栏推荐
- $. map(data,function(item,index){return XXX})
- DbNull if statement - DbNull if statement
- Maya foreground rendering plug-in Mel scripting tool
- 如何防止商場電氣火灾的發生?
- 2020-12-12
- Grafana data visualization
- cupp字典生成工具(同类工具还有crunch)
- Introduction to program environment and preprocessing C language (advanced level)
- The four pain points of enterprise digitalization are solved by low code platform
- GeForce GTX 2050/2080/3090/A6000自动安装nvidia显卡驱动
猜你喜欢

Application of residual pressure monitoring system in high-rise civil buildings

ssh公钥登录失败报错:sign_and_send_pubkey: no mutual signature supported

分数大小的比较

微信小程序项目实例——体质计算器

ARD3M电动机保护器在煤炭行业中的应用

Wechat applet project example - Fitness calculator

Application of ankery anti shake electric products in a chemical project in Hebei

2020-12-10

maya前台渲染插件mel脚本工具

How to build urban smart bus travel? Quick code to answer
随机推荐
Demand and business model innovation - demand 10- observation and document review
[digital signal processing] correlation function (periodic signal | autocorrelation function of periodic signal)
What is the commonly heard sub table of MySQL? Why does MySQL need tables?
Pydub MP3 to WAV
Start ticwatch2
2020-12-17
Unscrambling 2021 of service grid: bid farewell to the "great leap forward" of architecture, and a hundred schools of thought contend for the technological ecology
如何防止商場電氣火灾的發生?
Addition and multiplication of large integers;
Interpreting 2021 of middleware: after being reshaped by cloud nativity, it is more difficult to select models
maya前台渲染插件mel脚本工具
Comment prévenir les incendies électriques dans les centres commerciaux?
小红的删数字
Intel Galileo Gen2 development
Android HTML5 page load cache optimization
Requirements and business model innovation - Requirements 12 - process oriented modeling
laravel 8 选用 jwt 进行接口验证
Kubernetes' learning path. Is there any "easy mode" Q recommendation for container hybrid cloud
Graduation design of fire hydrant monitoring system --- thesis (add the most comprehensive hardware circuit design - > driver design - > Alibaba cloud Internet of things construction - > Android App D
余压监控系统在高层民用建筑的应用