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

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

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]

我试图实现的最终输出 -

Array(
[0] => Array
    (
        [0] => Array
            (
                [room_rate_key] => 0|0
                [amount] => 5307.84
            )
        [1] => Array
            (
                [room_rate_key] => 0|0
                [amount] => 5307.84
            )
    )
[1] => Array
    (
        [0] => Array
            (
                [room_rate_key] => 0|0
                [amount] => 5307.84
            )
        [1] => Array
            (
                [room_rate_key] => 0|1
                [amount] => 5246.98
            )
    )
[2] => Array
    (
        [0] => Array
            (
                [room_rate_key] => 0|1
                [amount] => 5246.98
            )
        [1] => Array
            (
                [room_rate_key] => 0|0
                [amount] => 5307.84
            )
    )
[3] => Array
    (
        [0] => Array
            (
                [room_rate_key] => 0|1
                [amount] => 5246.98
            )
        [1] => Array
            (
                [room_rate_key] => 0|1
                [amount] => 5246.98
            )
    )
)
英文:

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 -

Array(
[0] => Array
    (
        [0] => Array
            (
                [room_rate_key] => 0|0
                [amount] => 5307.84
            )
        [1] => Array
            (
                [room_rate_key] => 0|0
                [amount] => 5307.84
            )
    )
[1] => Array
    (
        [0] => Array
            (
                [room_rate_key] => 0|0
                [amount] => 5307.84
            )
        [1] => Array
            (
                [room_rate_key] => 0|1
                [amount] => 5246.98
            )
    )
[2] => Array
    (
        [0] => Array
            (
                [room_rate_key] => 0|1
                [amount] => 5246.98
            )
        [1] => Array
            (
                [room_rate_key] => 0|0
                [amount] => 5307.84
            )
    )
[3] => Array
    (
        [0] => Array
            (
                [room_rate_key] => 0|1
                [amount] => 5246.98
            )
        [1] => Array
            (
                [room_rate_key] => 0|1
                [amount] => 5246.98
            )
    )
)

答案1

得分: 2

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

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

package main

import "fmt"

type two struct {
    k1 int
    k2 int
}

func main() {
    v := make(map[two]two)

    v[two{1, 1}] = two{37, 38}
    v[two{0, 0}] = two{1, 1}

    fmt.Println(v)

}
英文:

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

to do a lookup use the struct as the key

package main

import "fmt"

type two struct {
	k1 int
	k2 int
}

func main() {
	v := make(map[two]two)

	v[two{1, 1}] = two{37, 38}
	v[two{0, 0}] = two{1, 1}

	fmt.Println(v)

}

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:

确定