如何从一个结构体切片构建结构树?

huangapple go评论75阅读模式
英文:

How to build a structure tree from a slice of struct?

问题

以下是您提供的代码的翻译结果:

这是我的初始模型:

type Team struct {
    Id    string `json:"id"`
    Name  string `json:"name"`
    Level int64  `json:"level"`
}

type TeamTree struct {
    Teams []Team     `json:"teams"`
    Child []TeamTree `json:"child"`
}

示例数据:

id     team      level
a      BOD         1
b      Marketing   2
c      Dev         2
d      Worker      3

我希望得到以下结果(应该是一个映射):

{
    "teams": [
        {"id": "a", "team": "BOD", "level": 1}
    ],
    "child": {
        "teams": [
            {"id":"b", "team": "marketing", "level": 2},
            {"id":"c", "team": "marketing", "level": 2}
        ],
        "child": {
            "teams": [
                {"id": "d", "team": "worker", "level": 3}
            ],
            "child": {}
        }
    }
}

有人告诉我要做自循环,但我不知道如何做,因为我一直遇到错误,我的代码甚至无法运行。

英文:

here are my initial model

type Team struct {
	Id    string `json:"id"`
	Name  string `json:"name"`
	Level int64  `json:"level"`
}

type TeamTree struct {
    Teams []Team     `json:"teams"`
 	Child []TeamTree `json:"child"`
}

Sample data:

 id     team      level
 a      BOD         1
 b      Marketing   2
 c      Dev         2
 d      Worker      3

I want to have this result (should be a mapping):

{
    "teams": [
        {"id": "a", "team": "BOD", "level": 1}
    ],
    "child": {
        "teams": [
            {"id":"b", "team": "marketing", "level": 2},
            {"id":"c", "team": "marketing", "level": 2}
        ],
        "child": {
            "teams": [
                {"id": "d", "team": "worker", "level": 3}
            ],
            "child"": {}
        }

    }
}

I was told to do self loop, but I do not know how to do it, since I keep getting error and my code cant even run.

答案1

得分: 2

这是你想要的一个完整示例:

package main

import "log"

type Team struct {
	id    string
	name  string
	level int64
}

type TeamTree struct {
	teams    []Team
	children []TeamTree
}

func main() {
	firstChild := TeamTree{teams: []Team{{id: "firstChildId", name: "First", level: 10}}, children: make([]TeamTree, 0)}
	secondChild := TeamTree{teams: []Team{{id: "secondChildId", name: "Second", level: 20}}, children: make([]TeamTree, 0)}
	thirdChild := TeamTree{teams: []Team{{id: "thirdChildId", name: "Third", level: 30}}, children: make([]TeamTree, 0)}
	rootTeamTree := TeamTree{teams: []Team{{id: "rootId", name: "Root", level: 100}}, children: []TeamTree{firstChild, secondChild, thirdChild}}

	log.Println("What's the second child's level:", rootTeamTree.children[1].teams[0].level)
}

这是结果:

2022/06/19 08:24:55 What's the second child's level: 20
英文:

This is a full example of what you want

package main

import "log"

type Team struct {
	id    string
	name  string
	level int64
}

type TeamTree struct {
	teams    []Team
	children []TeamTree
}

func main() {
	firstChild := TeamTree{teams: []Team{{id: "firstChildId", name: "First", level: 10}}, children: make([]TeamTree, 0)}
	secondChild := TeamTree{teams: []Team{{id: "secondChildId", name: "Second", level: 20}}, children: make([]TeamTree, 0)}
	thirdChild := TeamTree{teams: []Team{{id: "thirdChildId", name: "Third", level: 30}}, children: make([]TeamTree, 0)}
	rootTeamTree := TeamTree{teams: []Team{{id: "rootId", name: "Root", level: 100}}, children: []TeamTree{firstChild, secondChild, thirdChild}}

	log.Println("What's the second child's level:", rootTeamTree.children[1].teams[0].level)
}

And this is the result

2022/06/19 08:24:55 What's the second child's level: 20

huangapple
  • 本文由 发表于 2022年6月19日 11:14:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/72674051.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定