登录
首页 >  Golang >  Go问答

如何正确地将数据分组以获得树形结构?

来源:stackoverflow

时间:2024-04-06 09:03:40 348浏览 收藏

最近发现不少小伙伴都对Golang很感兴趣,所以今天继续给大家介绍Golang相关的知识,本文《如何正确地将数据分组以获得树形结构?》主要内容涉及到等等知识点,希望能帮到你!当然如果阅读本文时存在不同想法,可以在评论中表达,但是请勿使用过激的措辞~

问题内容

在golang应用程序中,我对这样的表进行查询:

| id        | agg_year | agg_month | gender | age_range | income_range | total |
|-----------|----------|-----------|--------|-----------|--------------|-------|
| 107502389 | 2019     | 7         | f      | 18_29     | 1000_2000    | 15    |
| 107502389 | 2019     | 7         | f      | 18_29     | 2000_4000    | 42    |
| 107502389 | 2019     | 7         | f      | 30_44     | 1000_2000    | 25    |
| 107502389 | 2019     | 7         | f      | 30_44     | 2000_4000    | 63    |
| 107502389 | 2019     | 7         | m      | 18_29     | 1000_2000    | 30    |
| 107502389 | 2019     | 7         | m      | 18_29     | 2000_4000    | 18    |
| 107502389 | 2019     | 7         | m      | 30_44     | 1000_2000    | 36    |
| 107502389 | 2019     | 7         | m      | 30_44     | 2000_4000    | 19    |

该表存储某月某工资水平的男性和女性总数信息。通常,对数据库进行查询后,每条记录都会被一一单独解析:

type entry struct {
    id int `json:"id"`
    aggyear int `json:"agg_year"`
    aggmonth int `json:"agg_month"`
    gender string `json:"gender"`
    agerange string `json:"age_range"`
    incomerange string `json:"income_range"`
    total int `json:"total"`
}

var entries []entry

rows, err := database.query("***"); if err != nil {
    fmt.println(err)
    return
}

defer rows.close()

for rows.next() {
    var entry entry

    if err = rows.scan(&entry.id, &entry.aggmethod, &entry.aggyear, &entry.aggmonth, &entry.gender, &entry.agerange, &entry.incomerange, &entry.total); err != nil {
        fmt.println(err)
        return
    }

    entries = append(entries, entry)
}

type incomedetails struct {
    incomerange string `json:"income_range"`
    total int `json:"total"`
}

type agedetails struct {
    agerange string `json:"age_range"`
    details []incomedetails `json:"details"`
}

type genderdetails struct {
    gender string `json:"gender"`
    details []agedetails `json:"details"`
}

type entrydetails struct {
    aggyear int `json:"agg_year"`
    aggmonth int `json:"agg_month"`
    details []genderdetails `json:"details"`
}

type dataentry struct {
    id int `json:"id"`
    details []entrydetails `json:"details"`
}

var entrydetails []entrydetails

i:= 0
for i < len(entries) {
    var genderdetails []genderdetails
    aggyear := entries[i].aggyear
    aggmonth := entries[i].aggmonth

    for aggyear == entries[i].aggyear && aggmonth == entries[i].aggmonth {
        gender := entries[i].gender
        var agedetails []agedetails
        for gender == entries[i].gender {
            agerange := entries[i].agerange
            var incomedetails []incomedetails

            for agerange == entries[i].agerange && gender == entries[i].gender { // <- runtime error: index out of range
                incomedetail := incomedetails{entries[i].incomerange, entries[i].total}
                incomedetails = append(incomedetails, incomedetail)
                i++
            }
            agedetail := agedetails{entries[i-1].agerange, incomedetails}
            agedetails = append(agedetails, agedetail)
            i++
        }
        genderdetail := genderdetails{entries[i-1].gender, agedetails}
        genderdetails = append(genderdetails, genderdetail)
        i++
    }
    entrydetail := entrydetails{entries[i-1].aggyear, entries[i-1].aggmonth, genderdetails}
    entrydetails = append(entrydetails, entrydetail)
    i++
}

我想知道对以下值进行分组的最佳方法,如下例所示?我想了解操作的顺序。我将不胜感激任何帮助。

[
    {
        "id": 107502389,
        "details": [
            {
                "agg_year": 2019,
                "agg_month": 7,
                "details": [
                    {
                        "gender": "F",
                        "details": [
                            {
                                "age_range": "18_29",
                                "details": [
                                    {
                                        "income_range": "1000_2000",
                                        "total": "15"
                                    },
                                    {
                                        "income_range": "2000_4000",
                                        "total": "42"
                                    },
                                ]
                            },
                            {
                                "age_range": "30_44",
                                "details": [
                                    {
                                        "income_range": "1000_2000",
                                        "total": "25"
                                    },
                                    {
                                        "income_range": "2000_4000",
                                        "total": "63"
                                    },
                                ]
                            }
                        ]
                    },
                    {
                        "gender": "M",
                        "details": [
                            {
                                "age_range": "18_29",
                                "details": [
                                    {
                                        "income_range": "1000_2000",
                                        "total": "30"
                                    },
                                    {
                                        "income_range": "2000_4000",
                                        "total": "18"
                                    },
                                ]
                            },
                            {
                                "age_range": "30_44",
                                "details": [
                                    {
                                        "income_range": "1000_2000",
                                        "total": "36"
                                    },
                                    {
                                        "income_range": "2000_4000",
                                        "total": "19"
                                    },
                                ]
                            }
                        ]
                    }
                ]
            }
        ]
    }
]

解决方案


通常,我会根据我想要的响应来更新结构。例如,在您的情况下,它将是:

type DataEntry struct {
    ID int `json:"id"`
    Details []EntryDetails `json:"details"`
}

type EntryDetails struct { 
    AggYear int `json:"agg_year"`
    AggMonth int `json:"agg_month"`
    Details []GenderDetails `json:"details"`
}

type GenderDetails struct {
    Gender string `json:"gender"`
    Details []AgeDetails `json:"details"`
} 

type AgeDetails struct {
    AgeRange string `json:"age_range"`
    Details []IncomeDetails `json:"details"`
}

type IncomeDetails struct { 
    IncomeRange string `json:"income_range"`
    Total int `json:"total"`
}

将代码分成更小的部分总是更容易阅读和维护。

下一部分将详细信息添加到结构中:您应该根据要求查询代码以一一填充结构。例如:首先是 getid-'entry struct',然后是 id-'entrydetails struct' 的 getaggyear 和 getaggmonth,依此类推。

您可以在这里找到完整的工作程序:https://play.golang.org/p/_pdb5y9Wd-O

享受吧!

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>