duanbangzhou7809 2017-07-25 12:02
浏览 139
已采纳

在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);
            }
        }
    }
  • 写回答

1条回答 默认 最新

  • douna1895 2017-07-25 12:37
    关注

    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})
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题