go 笛卡爾乘積 實現sku

1數據源

[
    [
        {2714 白色 0xc000180d80 0xc0000f1110 0xc000044e40} 
        {2715 屎黃色 0xc000180ea0 0xc0000f11f0 0xc000044ea0}
        {2720 綠色 0xc000180fc0 0xc0000f1340 0xc000045020}
    ] 
    [
        {2717 純棉 0xc000181320 0xc0000f1420 0xc000045380}
    ] 
    [
        {2716 30寸 0xc000181680 0xc0000f1500 0xc00099c2a0} 
        {2718 60寸 0xc0001817a0 0xc0000f15e0 0xc00099c300}
    ]
]

2代碼實現

func(this *GoodsController)GetSku(goods_attrs [][]models.GoodsAttr)([][]models.GoodsAttr){
	arrlen:=len(goods_attrs)//列
	sku:=make([][]models.GoodsAttr,0)
	for _,val :=range goods_attrs[0] {
		temps := make([]models.GoodsAttr, 0)
		temps = append(temps,val)
		sku =append(sku,temps)
	}
	for i:=0;i<arrlen-1;i++{
		skuarr:=make([][]models.GoodsAttr,0)
		for _,val:=range sku{
			for _,vals :=range goods_attrs[i+1]{
				temp:=make([]models.GoodsAttr,0)
				temp=append(temp,val...)
				temp=append(temp,vals)
				skuarr=append(skuarr,temp)
			}
		}
		sku=skuarr
	}
	return sku
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章