Go语言中的树结构

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

Go Tree structure in

问题

我在使用Go语言创建这种树形结构时遇到了一点问题。

{
    "uuid": "uuid1",
    "label": "label1",
    "children": [{
        "uuid": "uuid2",
        "label": "label2",
        "children": [{
            "uuid": "uuid3",
            "label": "label3",
            "children": [{
                "uuid": "uuid5",
                "label": "label5"
            }, {
                "uuid": "uuid7",
                "label": "label7"
            }]
        },
        {
            "uuid": "uuid4",
            "label": "label4",
            "children": [{
                "uuid": "uuid6",
                "label": "label6"
            }]
        }]
    }]
}

我尝试使用map来实现,但是在创建时遇到了问题。我构建的map看起来像这样:

Tree := make(map[string]map[string]map[string][]interface{})

数据将来自数据库,每一行的格式如下:

uuid level1 level2 level3 level4
uuid5 label5 label3 label2 label1
uuid7 label7 label3 label2 label1

希望有人能帮助我解决这个问题,我已经为此头疼了一阵子。

英文:

I have a little issue creating this kind of tree structure in go.

{
"uuid": "uuid1",
"label": "label1",
"children": [{
    "uuid": "uuid2",
    "label": "label2",
     "children": [{
        "uuid": "uuid3",
        "label": "label3",
            "children": [{
                "uuid": "uuid5",
                "label": "label5",
            },{
                "uuid": "uuid7",
                "label": "label7",
            }]
     },
     "uuid": "uuid4",
     "label": "label4",
            "children": [{
                "uuid": "uuid6",
                "label": "label6",
            }]
     }]
}]}

I tried it with maps, but I have a problem creating it. The map I came up with looked like this:

 Tree := make(map[string]map[string]map[string][]interface{})

The data would come from a database where every row looks something like this:

uuid level1 level2 level3 level4
uuid5 label5 label3 label2 label1
uuid7 label7 label3 label2 label1

I hope someone can help me with this, got some headache from that already.

答案1

得分: 3

根据Jason的建议,你的JSON树结构是错误的。我建议使用在线工具来验证JSON。

除此之外,你只需要为你的树结构定义一个结构体,如下所示。这是使用你的JSON文档在playground上的工作示例。

type Node struct {
	UUID      string `json:"uuid"`
	Label     string `json:"label"`
	Childrens []Node `json:"children"`
}

我们更喜欢使用structs而不是maps,因为它们是类型安全的。Go是一种强类型语言,使用结构体更符合惯例。

英文:

As suggested by Jason, your json tree structure is wrong. I would suggest using online tools for validating json's.

Apart from that, All you need to do here is to define a struct for your tree structure like below. Here is the working example on playground with your json document.

type Node struct {
	UUID      string `json:"uuid"`
	Label     string `json:"label"`
	Childrens []Node `json:"children"`
}

We prefer using structs over maps as they are typesafe. Go being a strongly typed language, using structs is more idiomatic.

答案2

得分: 0

嗯,我觉得你贴的树结构文本有错误。我调整了一下,像这样:

{
  "uuid": "uuid1",
  "label": "label1",
  "children": [
    {
      "uuid": "uuid2",
      "label": "label2",
      "children": [
        {
          "uuid": "uuid3",
          "label": "label3",
          "children": [
            {
              "uuid": "uuid4",
              "label": "label4"
            }
          ]
        }
      ]
    }
  ]
}

我建议使用Go结构体,而不是map。

两个月前,我发现了一个很好的网站,可以帮助将JSON格式的结构转换为Go结构体。

转换之前的代码如下:

type AutoGenerated struct {
	UUID     string `json:"uuid"`
	Label    string `json:"label"`
	Children []struct {
		UUID     string `json:"uuid"`
		Label    string `json:"label"`
		Children []struct {
			UUID     string `json:"uuid"`
			Label    string `json:"label"`
			Children []struct {
				UUID  string `json:"uuid"`
				Label string `json:"label"`
			} `json:"children"`
		} `json:"children"`
	} `json:"children"`
}
英文:

Well,I think you paste the text of tree structure is error。 I adjust it,like this:

{
  "uuid": "uuid1",
  "label": "label1",
  "children": [
    {
      "uuid": "uuid2",
      "label": "label2",
      "children": [
        {
          "uuid": "uuid3",
          "label": "label3",
          "children": [
            {
              "uuid": "uuid4",
              "label": "label4"
            }
          ]
        }
      ]
    }
  ]
}

I offer to use Go Struct, not map。

Before two months,I found a nice website, which can help to convert to Go struct from JSON format struct。

Before Convert:

type AutoGenerated struct {
	UUID     string `json:"uuid"`
	Label    string `json:"label"`
	Children []struct {
		UUID     string `json:"uuid"`
		Label    string `json:"label"`
		Children []struct {
			UUID     string `json:"uuid"`
			Label    string `json:"label"`
			Children []struct {
				UUID  string `json:"uuid"`
				Label string `json:"label"`
			} `json:"children"`
		} `json:"children"`
	} `json:"children"`
}

huangapple
  • 本文由 发表于 2021年7月24日 15:50:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/68508045.html
匿名

发表评论

匿名网友

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

确定