返回一个映射数组的Go语言代码。

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

returning an array of maps golang

问题

我正在尝试创建一个返回数组映射的函数。在Python中,我会返回一个字典列表。我觉得我可能漏掉了一些简单的东西,我不知道如何定义一个包含映射类型的数组变量。

以下是你提供的代码链接,我已经注释掉了不起作用的部分:

  1. https://go.dev/play/p/msPRp0WiaB1

我将其保留在main函数中作为示例,但我真正想做的是创建另一个函数,该函数返回映射列表,以便在代码的其他部分进行迭代:

  1. func theFarmInventory() []map[string]string {
  2. // 动物列表,这通常是在API调用中的循环中获取的
  3. animalList1 := []byte(`[{"Type":"Dog","Name":"Rover"},{"Type":"Cat","Name":"Sam"},{"Type":"Bird","Name":"Lucy"}]`)
  4. animalList2 := []byte(`[{"Type":"Hamster","Name":"Potato"},{"Type":"Rat","Name":"Snitch"},{"Type":"Cow","Name":"Moo"}]`)
  5. inventory1 := animalStock(animalList1)
  6. inventory2 := animalStock(animalList2)
  7. fmt.Printf("Inventory1 %v\nInventory2: %v\n", inventory1, inventory2)
  8. // 我想创建一个映射数组
  9. var theFarm []map[string]string
  10. theFarm = append(theFarm, inventory1)
  11. theFarm = append(theFarm, inventory2)
  12. fmt.Printf("%v", theFarm)
  13. return theFarm
  14. }

希望这可以帮助到你!

英文:

I'm trying to create an function that returns an array of maps. Or in python I would return a list of dicts for example. I think im missing something simple, I dont know how to define a variable of array with the type inside of it being maps.

Here's the working code I commented out the section that isn't working:

https://go.dev/play/p/msPRp0WiaB1

I left it in main for ^ example but what I really want to do is have another function that returns the list of maps so another part of the code and iterate over them:

  1. func theFarmInventory()[]map[string]map {
  2. //Lists of animals, this would normally be a loop in an api call for listsN
  3. animalList1 := []byte(`[{"Type":"Dog","Name":"Rover"},{"Type":"Cat","Name":"Sam"},{"Type":"Bird","Name":"Lucy"}]`)
  4. animalList2 := []byte(`[{"Type":"Hamster","Name":"Potato"},{"Type":"Rat","Name":"Snitch"},{"Type":"Cow","Name":"Moo"}]`)
  5. inventory1 := animalStock(animalList1)
  6. inventory2 := animalStock(animalList2)
  7. fmt.Printf("Inventory1 %v\nInventory2: %v\n", inventory1, inventory2)
  8. // I would like to create a array of maps
  9. var theFarm []map[string]string
  10. theFarm.append(theFarm,inventory1)
  11. theFarm.append(theFarm,inventory2)
  12. fmt.Printf("%v",theFarm)
  13. return theFarm
  14. }

答案1

得分: 3

使用内置的append函数将项目添加到切片中:

  1. var theFarm []map[string]string
  2. theFarm = append(theFarm, inventory1)
  3. theFarm = append(theFarm, inventory2)
  4. return theFarm

另一种选择是使用复合字面量

  1. theFarm := []map[string]string{inventory1, inventory2}
  2. return theFarm
英文:

Use the builtin append function to add items to the slice:

  1. var theFarm []map[string]string
  2. theFarm = append(theFarm, inventory1)
  3. theFarm = append(theFarm, inventory2)
  4. return theFarm

https://go.dev/play/p/4-588WdQ6mf

Another option is to use a composite literal:

  1. theFram := []map[string]string{inventory1, inventory2}
  2. return theFarm

https://go.dev/play/p/a7WZJOthwYt

huangapple
  • 本文由 发表于 2022年3月31日 09:28:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/71685855.html
匿名

发表评论

匿名网友

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

确定