-1

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 Answers1

2

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)

}
Vorsprung
  • 32,923
  • 5
  • 39
  • 63
  • Hi, Thank you for answer. But sorry I really could not understand. Will you be able to elaborate pls. – Bhargavi Pise Dec 02 '21 at 11:01
  • 1
    Please don't take this the wrong way, I would love to help. I looked at your go playground and example data. Unfortunately, to me, it is so inconsistent and buggy that it is not possible to say exactly what you are trying to do – Vorsprung Dec 02 '21 at 13:35