John Cargo
John Cargo

Reputation: 2121

Creating Multi dimensional Nested Map slice in Golang

TLDR; it's like storing value of child category as index of parent id in array. See equivalent PHP Code at end of block.

I need to store value of dad-daughter number. There are multiple person who are father and each father has multiple daughter. Some father may not have daughter either.So, I need to store value as :-

variableName[DadID][indexFrom0ToAvailableValue] = {"id": id, "name": name}

where indexFrom0toAvailableValue is index of number of daughter and their id and name.

What i am doing :-

patu := make(map[int][]map[string]string)

p,n := 0,0

for _,c := range  dadhu {
    // c.Daughter of value 2 means, current c is father
    if( c.Daughter == 2 ) {

        // Below i am using len(daddhu) to know
        // number of rows available. This is creating another bug
        // as it is creating lots of blank map.

        patu[c.DadID] = make([]map[string]string, len(dadhu))

        // Created array `patu` with `DadID` which will store 
        // their children below for range loop in above array

        p++
    }
}
fmt.Println("Total Father is : ", p)

for _,c := range dadhu {
    // c.Daughter of value 1 means, current c is daughter

    if ( c.Daughter == 1 ) {
        cID = strconv.Itoa(c.ID)
        patu[c.DadID][n] = map[string]string{"id": cID, "name" : c.Name}
        n++
    }
}

This is working fine, but my problem is, it is creating map like below :-

map[44:[map[] map[] map[] map[] map[id: 45 name:Lana] map[] map[] map[] map[] map[id:46 name:Mana] map[] map[] map[] map[] map[id: 47 name:Bana].........] 28:[[map[] map[] map[] map[] map[id: 29 name:Lana] map[] map[] map[] map[] map[id:30 name:Mana] map[] map[] map[] map[] map[id: 31 name:Bana]........map[] map[] map[] map[]]] 

and so on.

There are total 5 Parent ID and 49 Total number of Rows in mysql.

So, you can see, there are lots of blank map[] . Which get created. I need to clean this up. First 4 blank map each before storing daughter details. So daughter details are getting stored in 5th map . I am assuming if there will be 7 person who is father, than from 4 blank map it may become 6 blank map each.

I am not finding any logical mistake here except one where i am using len(dadhu) as i need to provide some value but i am not sure, which father has how many daughter.

Please let me know the alternate way to do this, if this is wrong in all ways in golang.

Just FYI : It's equivalent code in PHP - which is working fine is :

$patu = array();
$wod = array();
foreach ($dadhu as $e) {
    if ($e->isChild == '2') {
        $woc[] = $e->id;
        $patu[$e->id] = array();
    }
}
foreach($dadhu as $c) {
    if($c->isChild == '1') {
            if(in_array($c->dadID, $wod)) {
                $patu[$c->dadID][] = array($c->id, $c->name);
            }
        }
    }

Upvotes: 2

Views: 4806

Answers (1)

dev.bmax
dev.bmax

Reputation: 10601

Slices in Go are dynamically-sized, you shouldn't treat them like arrays.

In the first loop don't use len(dadhu) to initialize the slice if you don't know the exact size.

Instead, make an empty slice:

patu[c.DadID] = make([]map[string]string, 0)

In the second loop, append maps to it:

patu[c.DadID] = append(patu[c.DadID], map[string]string{"id": cID, "name": c.Name})

Upvotes: 4

Related Questions