Go语言中的树结构

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

Go Tree structure in

问题

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

  1. {
  2. "uuid": "uuid1",
  3. "label": "label1",
  4. "children": [{
  5. "uuid": "uuid2",
  6. "label": "label2",
  7. "children": [{
  8. "uuid": "uuid3",
  9. "label": "label3",
  10. "children": [{
  11. "uuid": "uuid5",
  12. "label": "label5"
  13. }, {
  14. "uuid": "uuid7",
  15. "label": "label7"
  16. }]
  17. },
  18. {
  19. "uuid": "uuid4",
  20. "label": "label4",
  21. "children": [{
  22. "uuid": "uuid6",
  23. "label": "label6"
  24. }]
  25. }]
  26. }]
  27. }

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

  1. 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.

  1. {
  2. "uuid": "uuid1",
  3. "label": "label1",
  4. "children": [{
  5. "uuid": "uuid2",
  6. "label": "label2",
  7. "children": [{
  8. "uuid": "uuid3",
  9. "label": "label3",
  10. "children": [{
  11. "uuid": "uuid5",
  12. "label": "label5",
  13. },{
  14. "uuid": "uuid7",
  15. "label": "label7",
  16. }]
  17. },
  18. "uuid": "uuid4",
  19. "label": "label4",
  20. "children": [{
  21. "uuid": "uuid6",
  22. "label": "label6",
  23. }]
  24. }]
  25. }]}

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

  1. 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上的工作示例。

  1. type Node struct {
  2. UUID string `json:"uuid"`
  3. Label string `json:"label"`
  4. Childrens []Node `json:"children"`
  5. }

我们更喜欢使用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.

  1. type Node struct {
  2. UUID string `json:"uuid"`
  3. Label string `json:"label"`
  4. Childrens []Node `json:"children"`
  5. }

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

答案2

得分: 0

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

  1. {
  2. "uuid": "uuid1",
  3. "label": "label1",
  4. "children": [
  5. {
  6. "uuid": "uuid2",
  7. "label": "label2",
  8. "children": [
  9. {
  10. "uuid": "uuid3",
  11. "label": "label3",
  12. "children": [
  13. {
  14. "uuid": "uuid4",
  15. "label": "label4"
  16. }
  17. ]
  18. }
  19. ]
  20. }
  21. ]
  22. }

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

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

转换之前的代码如下:

  1. type AutoGenerated struct {
  2. UUID string `json:"uuid"`
  3. Label string `json:"label"`
  4. Children []struct {
  5. UUID string `json:"uuid"`
  6. Label string `json:"label"`
  7. Children []struct {
  8. UUID string `json:"uuid"`
  9. Label string `json:"label"`
  10. Children []struct {
  11. UUID string `json:"uuid"`
  12. Label string `json:"label"`
  13. } `json:"children"`
  14. } `json:"children"`
  15. } `json:"children"`
  16. }
英文:

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

  1. {
  2. "uuid": "uuid1",
  3. "label": "label1",
  4. "children": [
  5. {
  6. "uuid": "uuid2",
  7. "label": "label2",
  8. "children": [
  9. {
  10. "uuid": "uuid3",
  11. "label": "label3",
  12. "children": [
  13. {
  14. "uuid": "uuid4",
  15. "label": "label4"
  16. }
  17. ]
  18. }
  19. ]
  20. }
  21. ]
  22. }

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:

  1. type AutoGenerated struct {
  2. UUID string `json:"uuid"`
  3. Label string `json:"label"`
  4. Children []struct {
  5. UUID string `json:"uuid"`
  6. Label string `json:"label"`
  7. Children []struct {
  8. UUID string `json:"uuid"`
  9. Label string `json:"label"`
  10. Children []struct {
  11. UUID string `json:"uuid"`
  12. Label string `json:"label"`
  13. } `json:"children"`
  14. } `json:"children"`
  15. } `json:"children"`
  16. }

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:

确定