业务场景:

一个机构查询科室信息的时候,希望返回树状结构的嵌套格式;

解决办法:

通过递归和指针,嵌套成对应的结构体;

借鉴了前人的代码,但是最后递归的指针调用自己也是调试了半天才出来,这里献上完整的示例代码.

package mainimport ("fmt""encoding/json"
)type dept struct {DeptId string `json:"deptId"`FrameDeptStr string `json:"frameDeptStr"`Child []*dept `json:"child"`
}
func main() {depts := make([]dept,0)var a depta.DeptId = "1"a.FrameDeptStr = ""depts = append(depts,a)a.DeptId="3"a.FrameDeptStr = "1"depts = append(depts,a)a.DeptId="4"a.FrameDeptStr = "1"depts = append(depts,a)a.DeptId="5"a.FrameDeptStr = "13"depts = append(depts,a)a.DeptId="6"a.FrameDeptStr = "13"depts = append(depts,a)fmt.Println(depts)deptRoots := make([]dept,0)for _,v := range depts{if v.FrameDeptStr == ""{deptRoots= append(deptRoots,v)}}pdepts := make([]*dept,0)for i,_ := range depts{var a *depta = &depts[i]pdepts = append(pdepts,a)}//获取了根上的科室fmt.Println("根上的科室有:",deptRoots)var node *deptnode = &depts[0]makeTree(pdepts,node)fmt.Println("the result we got is",pdepts)data, _ := json.Marshal(node)fmt.Printf("%s", data)}func has(v1 dept,vs []*dept) bool  {var has boolhas = falsefor _,v2 := range vs {v3 := *v2if v1.FrameDeptStr+v1.DeptId == v3.FrameDeptStr{has = truebreak}}return has}func makeTree(vs []*dept,node *dept) {fmt.Println("the node value in maketree is:",*node)childs := findChild(node,vs)fmt.Println(" the child we got is :",childs)for _,child := range childs{fmt.Println("in the childs's for loop, the child's address  here is:",&child)node.Child = append(node.Child,child)fmt.Println("in the child's for loop, after append the child is:",child)if has(*child,vs) {fmt.Println("i am in if has")fmt.Println("the child in if has is:",*child)fmt.Println("the child in if has 's address is:",child)makeTree(vs,child)}}}func findChild(v *dept,vs []*dept)(ret []*dept)  {for _,v2 := range vs{if v.FrameDeptStr+v.DeptId == v2.FrameDeptStr{ret= append(ret,v2)}}return
}

代码备注: 通过frame_dept_str来确定科室之间的关系的, (a.frame_dept_str= a's parent's frame_dept_str + a's parent's dept_id).

golang通过递归遍历生成树状结构相关推荐

  1. 使用递归遍历树状结构数据

    递归遍历树状结构 datas:[{id: 1,name: '分组1',expand: true,type:'group',editable:false,active:false,children: [ ...

  2. vue递归生成树状结构

    生成前 生成后 console.log打印 全部实现代码片段(附备注 <template><divclass="container"style="fon ...

  3. 根据ParentId生成树状结构这po事

    最近项目里老是碰到数据库里存放父子级关系,然后获取出来要根据ParentId生成树状结构对象的需要. 也不知道最近是脑子不行还是咋滴.这po事想了老半天才想明白怎么去构建该结构.555555最后敲出来 ...

  4. Java生成树状结构返回结果

    树状结果集返回以及收集部分信息 对应的表结构和业务需求如下图所示 实现思路:递归业务代码 1.树状结构结果集返回 实现结果 业务代码 分析此处是先查询出根节点,暂定最大的FAQ.然后设置一个实体作为返 ...

  5. Python操作Excel(将父子级表头生成树状结构)

    import reclass Node:'''容器,用来存储前后节点信息'''__slot__=[]def __init__(self,val,next_,pre,name,no):self.val ...

  6. Vue中使用z-tree处理大数量的数据,生成树状结构

    ✍️ 作者简介: 一个每天中午去打篮球和锻炼的前端开发.

  7. [递归+访问者模式]实现树状结构的节点遍历处理

    /*** 把嵌套树结构 Tree 转成 Graph 图结构** @param tree Tree* @return Graph*/public static GraphVO tree2graph(It ...

  8. Java递归子集算法(树状结构)的逻辑和实例代码实现 @杨章隐

    Java递归算法(树状结构)的逻辑和实例 1.应用场景: 递归算法作为一个经常使用的算法,无论在API开发还是计算文件夹都是比较常用的, 在api开发过程中我们经常遇到需要返回树状结构的json 例如 ...

  9. Stream流实现Tree树状结构无限递归

    Stream流实现Tree树状结构无限递归 场景:在使用mybatis plus操作多层数据格式,多层遍利操作影响数据查询效率 代码 例子:需要返回给前端多级菜单 省(直辖市)/市/区(县) /*** ...

最新文章

  1. 2 分 31 秒,腾讯云创造 128 卡训练 ImageNet 新记录
  2. 爱了爱了!0.052 秒打开 100GB 数据,这个Python开源库火爆了!
  3. java简单springboot系统_Springboot系列 3 - 建立简单的用户登录系统
  4. ASP.NET生成缩略图类C#代码
  5. 转载大神的一篇文章----【如何选择开源许可证?】
  6. c语言输出单链表最大值与最小值,数据结构(C语言版)---顺序表与链表的比较...
  7. JPA实体注解与hibernate主键生成策略
  8. java字符后移_java把字符串参数往后移3位后输出
  9. Python3 List sort(),sorted(),reverse()的使用及辨别
  10. rbw数字信号处理_数字中频概述 - 频谱分析
  11. javac 编译JavaWeb项目
  12. 京东自动签到领京豆软件_京东618来了,合法又简单的自动领京豆的技能要get一下吗?...
  13. 【学术】论文返修时,能否增加作者或变更作者顺序?
  14. 小鑫鑫的平均分(排序问题)
  15. JAVA instrument简单使用
  16. EF(EntityFramework) 的 CodeFirst 使用指南二(基本使用)
  17. 再见Python你好C语言,再见,Python 2 你好,Python 3
  18. 《高等数学》 总结 导数、微分、不定积分
  19. ! LaTeX Error: File xxx.sty not found-统一解决办法
  20. 分子间相互作用——偶极作用dipolar-dopolar interaction

热门文章

  1. No Brainer sdut acm
  2. 模仿超级课程表——抓取学校课表数据
  3. As常用的公式(留着)
  4. 【vueCms】vueCms_xg后台管理系统(开源)
  5. SpringBoot实现CURD
  6. 资料整理:mac机械键盘设置(Karabiner 与 Karabiner-Element)
  7. Python模块subprocess小记
  8. Python深度学习-Data Augmentation:使用Augly库进行图片数据增强
  9. redmine 邮箱配置(阿里云+windows)
  10. 用PHP程序来查询数据库内容并以表格的形式输出