分配到nil映射中的条目,初始化映射没有

时间:2018-04-08 07:26:28

标签: go

下面的代码给了我“在nil map中输入的参与”错误,我搜索了这个错误,很多回答说我需要初始化我的地图,我试图初始化地图需要为“need:= make(map [map] string] Item)“它仍然给我同样的错误, b.ingredients返回一种map [string] Item,这里我的错误是什么?

func (b *Bread) ShoppingList(have map[string]Item) (difference map[string]Item, left map[string]Item) {

    need := b.ingredients

    for k, v := range need {
        flag := false
        for key, value := range have {
            if key == k {
                flag = true
                if value.weight > v.weight {
                    left[key] = Item{value.weight - v.weight}
                } else {
                    difference[key] = Item{v.weight - value.weight}
                    left[key] = Item{0}
                }
                break
            }
        }
        if !flag {
            need[k] = v
        }
    }

    return
}

type Bread struct {
    name        string
    ingredients map[string]Item
    weight      float32
    Baking
}

type Item struct {
    weight int
}
func main() {
have := map[string]Item{"Whole wheat flour": Item{5000}, "sugar": Item{1000}, "salt": {500}}

tmp := map[string]Item{"white flour": Item{200}, "sesame": Item{50}}
tmp2 := map[string]Item{"Whole wheat flour": Item{250}}
slices := [...]Baker{NewBread(), NewBreadVariation("Sesame bread", tmp, tmp2)}
slices[0].PrintBreadInfo()
slices[1].PrintBreadInfo()
difference, left := slices[0].ShoppingList(have)
//differencce1, _ := slices[1].ShoppingList(left)

fmt.Print(difference)
fmt.Print(left)
fmt.Print(have)
//fmt.Print(differencce1)
}


type Baker interface {
ShoppingList(map[string]Item) (map[string]Item, map[string]Item)
PrintBakeInstructions()
PrintBreadInfo()
}

func NewBread() (brd *Bread) {

ing := map[string]Item{"Whole wheat flour": Item{500}, "yeast": Item{25}, "salt": Item{25},
    "sugar": Item{50}, "butter": Item{50}, "water": Item{350}}
bk := Baking{120, 60, 180}
brd = &Bread{"Whole wheat", ing, 1.000, bk}
return
}

func NewBreadVariation(name string, add map[string]Item, removed map[string]Item) (brd *Bread) {
var weight float32
bk := Baking{120, 60, 180}
origin := map[string]Item{"Whole wheat flour": Item{500}, "yeast": Item{25}, "salt": Item{25},
    "sugar": Item{50}, "butter": Item{50}, "water": Item{350}}

if add != nil {
    for k, v := range add {
        flag := false
        for key, value := range origin {

            if key == k {
                flag = true
                origin[key] = Item{v.weight + value.weight}
                break
            }

        }

        if !flag {
            origin[k] = v
        }

    }
}
if removed != nil {
    for k, v := range removed {
        flag2 := false
        for key, value := range origin {
            if k == key {
                flag2 = true
                origin[key] = Item{value.weight - v.weight}
                break
            }
        }

        if !flag2 {
            fmt.Print("something wrong")
        }

    }
}

for _, v := range origin {
    tmp := v.weight
    tmp2 := float32(tmp)
    weight += tmp2
}
weight2 := weight / 1000

return &Bread{name, origin, weight2, bk}
}

1 个答案:

答案 0 :(得分:1)

您在ShoppingList函数中使用了命名返回值,因此differenceleft 声明了:它们被设置为类型'零值。对于地图,这是nilsee spec) - 当然,将项目附加到nil地图会导致恐慌。

因此,在向其添加任何项目之前,请执行以下操作为其分配值:

difference = make(map[string]Item)
left = make(map[string]Item)