Generate map with one to many relation

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]

However, I am unable to achieve it.
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
            )
    )
)

Can someone please help. ?

Thanks .

1 Like

Hello @bhargavi.pise ,

This line has a typo:

for i := 0; i < 1; i++ {

Secondly, I do not understand what do you mean by “generate a one to many relationship map from existing map”. I see in your example that you have a map with int keys and map[string]string values:

	mm := map[int]map[string]string{
		0: map[string]string{
			"amount":        "100.00",
			"room_rate_key": "0|0",
		},
		1: map[string]string{
			"amount":        "200.00",
			"room_rate_key": "0|1",
		},
	}

What do they mean logically, what are you trying to obtain ?

Hello @telo_tade ,

From one map shared on playground, I am trying to achieve new map in such a way that each element of main map i.e. [0]th will be mapped with all elements of [1]th element.

Hi @bhargavi.pise ,

Let me see if I understand correctly: your start with an initial map called “main map”, or shortly mm:

	mm := map[int]map[string]string{
		0: map[string]string{
			"amount":        "100.00",
			"room_rate_key": "0|0",
		},
		1: map[string]string{
			"amount":        "200.00",
			"room_rate_key": "0|1",
		},
	}

Out of this initial map (mm), you want to obtain another data structure, also a map. Lets call this data structure “result”.

	result:= map[int][]map[int]map[string]string{
		0: []map[int]map[string]string{
			{
				0: map[string]string{
					"amount":        "100.00",
					"room_rate_key": "0|0",
				},
				1: map[string]string{
					"amount":        "200.00",
					"room_rate_key": "0|1",
				},
			},
		1: []map[int]map[string]string{
			{
				0: map[string]string{
					"amount":        "100.00",
					"room_rate_key": "0|0",
				},
				1: map[string]string{
					"amount":        "200.00",
					"room_rate_key": "0|1",
				},
			},
		2: []map[int]map[string]string{
			{
				0: map[string]string{
					"amount":        "100.00",
					"room_rate_key": "0|0",
				},
				1: map[string]string{
					"amount":        "200.00",
					"room_rate_key": "0|1",
				},
			},
		3: []map[int]map[string]string{
			{
				0: map[string]string{
					"amount":        "100.00",
					"room_rate_key": "0|0",
				},
				1: map[string]string{
					"amount":        "200.00",
					"room_rate_key": "0|1",
				},
			},
		},
	}

Is this the correct definition of result ? Can you post the Golang definition of the result, it would help make things clearer.

Hello @telo_tade
Yes. This is correct.

Well, is it not sufficient then to create a and array of the appropriate size, then fill it in with copies of mm ? The solution posted in the playground seems a bit over engineered.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.