go - Creating Multi dimensional Nested Map slice in Golang -


tldr; it's storing value of child category index of parent id in array. see equivalent php code @ end of block.

i need store value of dad-daughter number. there multiple person father , each father has multiple daughter. father may not have daughter either.so, need store value :-

variablename[dadid][indexfrom0toavailablevalue] = {"id": id, "name": name} 

where indexfrom0toavailablevalue index of number of daughter , id , name.

what doing :-

patu := make(map[int][]map[string]string)  p,n := 0,0  _,c := range  dadhu {     // c.daughter of value 2 means, current c father     if( c.daughter == 2 ) {          // below using len(daddhu) know         // number of rows available. creating bug         // creating lots of blank map.          patu[c.dadid] = make([]map[string]string, len(dadhu))          // created array `patu` `dadid` store          // children below range loop in above array          p++     } } fmt.println("total father : ", p)  _,c := range dadhu {     // c.daughter of value 1 means, current c daughter      if ( c.daughter == 1 ) {         cid = strconv.itoa(c.id)         patu[c.dadid][n] = map[string]string{"id": cid, "name" : c.name}         n++     } } 

this working fine, problem is, creating map 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 on.

there total 5 parent id , 49 total number of rows in mysql.

so, can see, there lots of blank map[] . created. need clean up. first 4 blank map each before storing daughter details. daughter details getting stored in 5th map . assuming if there 7 person father, 4 blank map may become 6 blank map each.

i not finding logical mistake here except 1 using len(dadhu) need provide value not sure, father has how many daughter.

please let me know alternate way this, if wrong in ways in golang.

just fyi : it's equivalent code in php - working fine :

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

slices in go dynamically-sized, shouldn't treat them arrays.

in first loop don't use len(dadhu) initialize slice if don't know exact size.

instead, make empty slice:

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

in second loop, append maps it:

patu[c.dadid] = append(patu[c.dadid], map[string]string{"id": cid, "name": c.name}) 

Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -