集合初始化映射(转围)

下面的Gorang代码根据以下形式的输入为每个食谱收集了一组营养:


# applepie

- flour

- apple

- egg

# pizza

- flour

- cheese

- egg

- tomato

它在 (2) 中抱怨。assignment to entry in nil map


但是为什么?然而,每个“子映射”都初始化为 at (1) ?make


package main


import (

    "bufio"

    "fmt"

    "os"

    "regexp"

)


func main() {

    nutriments := map[string][]string{

            "flour":{"sugars"},

            "egg":{"protein", "fat"},

            "tomato":{"water", "viamins"},

            "cheese":{"calcium", "protein"},

            "apple":{"sugars", "fiber", "vitamin"} }

    total := make(map[string]map[string]bool)

    f := bufio.NewScanner(os.Stdin)

    currentRecipe := ""

    for f.Scan() {

            Line(f.Text(), currentRecipe, nutriments, total)

    }

    fmt.Println(total)

}


func Line (line, currentRecipe string, nut map[string][]string, tot map[string]map[string]bool) {

    if foundrec, _ := regexp.MatchString("^# [[:graph:]]+", line); foundrec { // Begin recipe

            currentRecipe := line[2:]

            fmt.Println("---------", currentRecipe) // update current

            tot[currentRecipe] = make(map[string]bool) // <== INITIALIZE SET (1)

    }

    if foundnut, _ := regexp.MatchString("^- [[:graph:]]+", line); foundnut { // New ingredient

            nutri := line[2:]

            tot[currentRecipe][nutri] = true   // <===== HERE (2)

    }

}


千巷猫影
浏览 67回答 1
1回答

浮云间

正如评论所指出的,问题在于当前Recipe的更新机制。字符串按值传递。这是正确的代码package mainimport (&nbsp; &nbsp; "bufio"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "os"&nbsp; &nbsp; "regexp")func main() {&nbsp; &nbsp; nutriments := map[string][]string{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "flour":{"sugars"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "egg":{"protein", "fat"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "tomato":{"water", "viamins"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "cheese":{"calcium", "protein"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "apple":{"sugars", "fiber", "vitamin"} }&nbsp; &nbsp; total := make(map[string]map[string]bool)&nbsp; &nbsp; f := bufio.NewScanner(os.Stdin)&nbsp; &nbsp; currentRecipe := ""&nbsp; &nbsp; for f.Scan() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentRecipe = Line(f.Text(), currentRecipe, nutriments, total)&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println(total)}func Line (line, currentRecipe string, nut map[string][]string, tot map[string]map[string]bool) string {&nbsp; &nbsp; if foundrec, _ := regexp.MatchString("^# [[:graph:]]+", line); foundrec { // Begin recipe&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentRecipe = line[2:]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("---------", currentRecipe) // update current&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tot[currentRecipe] = make(map[string]bool) // prepare set&nbsp; &nbsp; }&nbsp; &nbsp; if foundnut, _ := regexp.MatchString("^- [[:graph:]]+", line); foundnut { // Begin ingredients list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nutri := line[2:]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tot[currentRecipe][nutri] = true&nbsp; &nbsp; }&nbsp; &nbsp; return currentRecipe}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go