Golang – 生成具有一对多关系的映射

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

Golang - Generate map with one to many relation

问题

我对golang还比较新手,我正在努力从现有的映射中生成一个一对多的关系映射。

这是我的脚本playground

解释:
我试图实现将第0个位置的每个元素与第1个、第2个...第n个位置的每个元素建立关系。

例如 - [0][0]=>[1][0], [0][0]=>[1][1], [0][1]=>[1][0], [0][1]=>[1][1], [0][0]=>[2][0], [0][1]=>[2][1]

我试图实现的最终输出 -

  1. Array(
  2. [0] => Array
  3. (
  4. [0] => Array
  5. (
  6. [room_rate_key] => 0|0
  7. [amount] => 5307.84
  8. )
  9. [1] => Array
  10. (
  11. [room_rate_key] => 0|0
  12. [amount] => 5307.84
  13. )
  14. )
  15. [1] => Array
  16. (
  17. [0] => Array
  18. (
  19. [room_rate_key] => 0|0
  20. [amount] => 5307.84
  21. )
  22. [1] => Array
  23. (
  24. [room_rate_key] => 0|1
  25. [amount] => 5246.98
  26. )
  27. )
  28. [2] => Array
  29. (
  30. [0] => Array
  31. (
  32. [room_rate_key] => 0|1
  33. [amount] => 5246.98
  34. )
  35. [1] => Array
  36. (
  37. [room_rate_key] => 0|0
  38. [amount] => 5307.84
  39. )
  40. )
  41. [3] => Array
  42. (
  43. [0] => Array
  44. (
  45. [room_rate_key] => 0|1
  46. [amount] => 5246.98
  47. )
  48. [1] => Array
  49. (
  50. [room_rate_key] => 0|1
  51. [amount] => 5246.98
  52. )
  53. )
  54. )
英文:

I am fairly new to golang and I am struggling to generate a one to many relationship map from existing map.

Here is my script playground

Explanation:-
I am trying to achieve the relation of each element of 0th position to each element of 1st,2nd,...nth position.

For example - [0][0]=>[1][0], [0][0]=>[1][1], [0][1]=>[1][0], [0][1]=>[1][1], [0][0]=>[2][0], [0][1]=>[2][1]

Final Output which I am trying to achieve -

  1. Array(
  2. [0] => Array
  3. (
  4. [0] => Array
  5. (
  6. [room_rate_key] => 0|0
  7. [amount] => 5307.84
  8. )
  9. [1] => Array
  10. (
  11. [room_rate_key] => 0|0
  12. [amount] => 5307.84
  13. )
  14. )
  15. [1] => Array
  16. (
  17. [0] => Array
  18. (
  19. [room_rate_key] => 0|0
  20. [amount] => 5307.84
  21. )
  22. [1] => Array
  23. (
  24. [room_rate_key] => 0|1
  25. [amount] => 5246.98
  26. )
  27. )
  28. [2] => Array
  29. (
  30. [0] => Array
  31. (
  32. [room_rate_key] => 0|1
  33. [amount] => 5246.98
  34. )
  35. [1] => Array
  36. (
  37. [room_rate_key] => 0|0
  38. [amount] => 5307.84
  39. )
  40. )
  41. [3] => Array
  42. (
  43. [0] => Array
  44. (
  45. [room_rate_key] => 0|1
  46. [amount] => 5246.98
  47. )
  48. [1] => Array
  49. (
  50. [room_rate_key] => 0|1
  51. [amount] => 5246.98
  52. )
  53. )
  54. )

答案1

得分: 2

使用一个包含两个值的结构体作为地图的键

使用该结构体作为键进行查找

  1. package main
  2. import "fmt"
  3. type two struct {
  4. k1 int
  5. k2 int
  6. }
  7. func main() {
  8. v := make(map[two]two)
  9. v[two{1, 1}] = two{37, 38}
  10. v[two{0, 0}] = two{1, 1}
  11. fmt.Println(v)
  12. }
英文:

use a struct with two values as the key for the map

to do a lookup use the struct as the key

  1. package main
  2. import "fmt"
  3. type two struct {
  4. k1 int
  5. k2 int
  6. }
  7. func main() {
  8. v := make(map[two]two)
  9. v[two{1, 1}] = two{37, 38}
  10. v[two{0, 0}] = two{1, 1}
  11. fmt.Println(v)
  12. }

huangapple
  • 本文由 发表于 2021年12月1日 22:23:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/70185938.html
匿名

发表评论

匿名网友

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

确定