分析分层数据结构的方法
来源:stackoverflow
2024-03-10 11:12:27
0浏览
收藏
哈喽!今天心血来潮给大家带来了《分析分层数据结构的方法》,想必大家应该对Golang都不陌生吧,那么阅读本文就都不会很困难,以下内容主要涉及到,若是你正在学习Golang,千万别错过这篇文章~希望能帮助到你!
问题内容
在oracle中,我有这样的表:
| organization_id | ogranization_name | parent_id | level | |-----------------|-------------------|-----------|-------| | 1 | facebook | | 0 | | 2 | instagram | 1 | 1 | | 3 | whatsapp | 1 | 1 | | 4 | dynamic | 2 | 2 | | 5 | google | | 0 |
此表显示组织的层次结构。我需要在我的 go 应用程序中创建一个树状嵌套 json 响应:
[
{
"organization_id": 1
"ogranization_name": "facebook",
"childs": [
{
"organization_id": 2,
"ogranization_name": "instagram",
"childs": null
},
{
"organization_id": 3,
"ogranization_name": "whatsapp",
"childs": [
{
"organization_id": 4,
"ogranization_name": "dynamic",
"childs": null
}
]
}
]
},
{
"organization_id": 5
"ogranization_name": "google",
"childs": null
}
]
我向 oracle 数据库进行查询。我还创建了结构体。我的问题是如何知道一个组织是否有孩子?
main.go:
type Organisation struct {
ID int `json:"organization_id"`
Name string `json:"ogranization_name"`
Children []Organisation `json:"childs"`
}
rows,err := db.Query("select * from ORG_TABLE")
if err != nil {
fmt.Println(err)
return
}
var organizations []Organization
for rows.Next() {
var organization Organization
err = rows.Scan(&organization.ID, &organization.Name, ???)
if err != nil {
fmt.Println(err)
return
}
}
organizations = append(organizations, organization)解决方案
最后,我找到了解决方案:
package main
import (
"database/sql"
"encoding/json"
"fmt"
"log"
"sort"
_ "github.com/mattn/go-sqlite3"
)
type Organization struct {
ID int `json:"organization_id"`
Name string `json:"organization_name"`
Rang int `json:"organization_rang"`
Children []*Organization `json:"children"`
}
func main() {
db, err := sql.Open("sqlite3", "./database")
if err != nil {
log.Fatal(err)
}
defer db.Close()
rows, err := db.Query("select ORGANIZATION_ID,ORGANIZATION_NAME,ORGANIZATION_RANG,PARENT_ORGANIZATION_ID from ORG")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
orgs := map[int]*Organization{}
for rows.Next() {
organization := &Organization{}
var parentID sql.NullInt64
if err = rows.Scan(&organization.ID, &organization.Name, &organization.Rang, &parentID); err != nil {
log.Fatal(err)
}
if parentID.Valid {
if parentOrg, ok := orgs[int(parentID.Int64)]; ok {
parentOrg.Children = append(parentOrg.Children, organization)
} else {
orgs[int(parentID.Int64)] = &Organization{ID: int(parentID.Int64)}
orgs[int(parentID.Int64)].Children = append(orgs[int(parentID.Int64)].Children, organization)
}
}
if _, ok := orgs[organization.ID]; ok {
orgs[organization.ID].Name = organization.Name
orgs[organization.ID].Rang = organization.Rang
continue
}
orgs[organization.ID] = organization
}
IDs := []int{}
for k := range orgs {
IDs = append(IDs, k)
}
sort.Ints(IDs)
organizations := []Organization{}
for _, ID := range IDs {
if len(orgs[ID].Children) > 0 && orgs[ID].Rang == 1 {
organizations = append(organizations, *orgs[ID])
}
}
content, err := json.MarshalIndent(organizations, "", " ")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(content))
}以上就是《分析分层数据结构的方法》的详细内容,更多关于的资料请关注golang学习网公众号!
版本声明
本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
处理Oracle数据导入中文乱码问题的方法
- 上一篇
- 处理Oracle数据导入中文乱码问题的方法
- 下一篇
- 创建复杂结构的实例
查看更多
最新文章
-
- Golang · Go问答 | 3小时前 |
- Go utf8.DecodeRuneInString 为什么会返回 RuneError 和 1
- 298浏览 收藏
-
- Golang · Go问答 | 3小时前 | Go教程 · Go 自定义Handler slog.Record NumAttrs Attrs
- Go slog.Record.NumAttrs 与 Attrs 遍历结果有什么区别
- 459浏览 收藏
-
- Golang · Go问答 | 3小时前 | go · 源码分析 · Go ast go/doc.Examples 示例排序
- Go go/doc.Examples 的排序怎样按示例名称稳定输出
- 351浏览 收藏
-
- Golang · Go问答 | 4小时前 |
- Go 循环里的 defer 为什么没有按预期释放
- 311浏览 收藏
-
- Golang · Go问答 | 4小时前 | go · 垃圾回收 · Go问答 · Go runtime.MemStats HeapAlloc HeapReleased HeapInuse HeapSys
- Go MemStats HeapAlloc 与 HeapInuse 有什么区别
- 439浏览 收藏
查看更多
课程推荐
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
查看更多
AI推荐
-
- PubMedQA
- 深入了解PubMedQA生物医学问答数据集,涵盖其核心功能、使用方法及在临床决策、药物研发等场景的应用,助力提升NLP模型性能。
- 1029次使用
-
- H2O EvalGPT
- H2O EvalGPT是H2O.ai推出的开源LLM评估平台,提供详细的大模型性能排行榜、行业特定基准测试及A/B测试功能,助您快速选择最适合项目的高性能大语言模型。
- 1030次使用
-
- LMArena
- LMArena是加州大学伯克利分校推出的AI模型匿名评测平台。通过盲测投票机制,用户可对比不同大模型回答并生成实时排行榜,助力开发者优化模型及用户选择最佳AI工具。
- 1173次使用
-
- HELM
- 深入了解斯坦福推出的HELM(Holistic Evaluation of Language Models)大模型评测体系。本文解析其核心功能、安装配置步骤及应用场景,涵盖准确性、公平性、鲁棒性等多维度指标,助力开发者全面优化语言模型性能。
- 1133次使用
-
- MMBench
- MMBench是由上海人工智能实验室等机构联合推出的多模态基准测试平台,提供细粒度能力评估、大规模数据集及VLMEvalKit工具。本文详细介绍其核心功能、安装使用方法及应用场景,助力开发者全面评估多模态模型性能。
- 973次使用
查看更多
相关文章
-
- 用Nginx反向代理部署go写的网站。
- 2023-01-17 502浏览
-
- GoLand调式动态执行代码
- 2023-01-13 502浏览
-
- Go pprof profile duration 参数如何理解
- 2026-11-14 501浏览
-
- Go types.Selection.Type 与 Obj.Type 为什么不总是相同
- 2026-11-10 501浏览
-
- Go flag.FlagSet ContinueOnError 怎么捕获帮助输出和解析错误
- 2026-11-05 501浏览

