集合映射的初始化(Go)

huangapple go评论82阅读模式
英文:

map of sets initialization (Go)

问题

以下是翻译好的内容:

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

# applepie
- 面粉
- 苹果
- 鸡蛋
# pizza
- 面粉
- 奶酪
- 鸡蛋
- 番茄

它在 (2) 处报错 assignment to entry in nil map

但是为什么呢?每个“子映射”在 (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 { // 开始食谱
        currentRecipe := line[2:]
        fmt.Println("---------", currentRecipe) // 更新当前食谱
        tot[currentRecipe] = make(map[string]bool) // <== 初始化集合 (1)
    }
    if foundnut, _ := regexp.MatchString("^- [[:graph:]]+", line); foundnut { // 新成分
        nutri := line[2:]
        tot[currentRecipe][nutri] = true   // <===== 这里 (2)
    }
}
英文:

Below Golang code gathers a set of nutriments for each recipe, based on an input of this form:

# applepie
- flour
- apple
- egg
# pizza
- flour
- cheese
- egg
- tomato

It complains at (2) of assignment to entry in nil map.

But why ? Each "submap" is howhever initialized with the make at (1) ?

package main

import (
    &quot;bufio&quot;
    &quot;fmt&quot;
    &quot;os&quot;
    &quot;regexp&quot;
)

func main() {
    nutriments := map[string][]string{
            &quot;flour&quot;:{&quot;sugars&quot;},
            &quot;egg&quot;:{&quot;protein&quot;, &quot;fat&quot;},
            &quot;tomato&quot;:{&quot;water&quot;, &quot;viamins&quot;},
            &quot;cheese&quot;:{&quot;calcium&quot;, &quot;protein&quot;},
            &quot;apple&quot;:{&quot;sugars&quot;, &quot;fiber&quot;, &quot;vitamin&quot;} }
    total := make(map[string]map[string]bool)
    f := bufio.NewScanner(os.Stdin)
    currentRecipe := &quot;&quot;
    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(&quot;^# [[:graph:]]+&quot;, line); foundrec { // Begin recipe
            currentRecipe := line[2:]
            fmt.Println(&quot;---------&quot;, currentRecipe) // update current
            tot[currentRecipe] = make(map[string]bool) // &lt;== INITIALIZE SET (1)
    }
    if foundnut, _ := regexp.MatchString(&quot;^- [[:graph:]]+&quot;, line); foundnut { // New ingredient
            nutri := line[2:]
            tot[currentRecipe][nutri] = true   // &lt;===== HERE (2)
    }
}

答案1

得分: 1

根据评论中指出的问题,问题出在对currentRecipe的更新机制上。字符串是按值传递的。

以下是正确的代码:

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)
    var currentRecipe string
    for f.Scan() {
        currentRecipe = Line(f.Text(), currentRecipe, nutriments, total)
    }
    fmt.Println(total)
}

func Line(line, currentRecipe string, nut map[string][]string, tot map[string]map[string]bool) string {
    if foundrec, _ := regexp.MatchString("^# [[:graph:]]+", line); foundrec { // Begin recipe
        currentRecipe = line[2:]
        fmt.Println("---------", currentRecipe) // update current
        tot[currentRecipe] = make(map[string]bool) // prepare set
    }
    if foundnut, _ := regexp.MatchString("^- [[:graph:]]+", line); foundnut { // Begin ingredients list
        nutri := line[2:]
        tot[currentRecipe][nutri] = true
    }
    return currentRecipe
}

希望对你有帮助!

英文:

As pointed in comments, problem was lying in the update mechanism for currentRecipe. string was passed by value.

Here is correct code

package main

import (
    &quot;bufio&quot;
    &quot;fmt&quot;
    &quot;os&quot;
    &quot;regexp&quot;
)

func main() {
    nutriments := map[string][]string{
            &quot;flour&quot;:{&quot;sugars&quot;},
            &quot;egg&quot;:{&quot;protein&quot;, &quot;fat&quot;},
            &quot;tomato&quot;:{&quot;water&quot;, &quot;viamins&quot;},
            &quot;cheese&quot;:{&quot;calcium&quot;, &quot;protein&quot;},
            &quot;apple&quot;:{&quot;sugars&quot;, &quot;fiber&quot;, &quot;vitamin&quot;} }
    total := make(map[string]map[string]bool)
    f := bufio.NewScanner(os.Stdin)
    currentRecipe := &quot;&quot;
    for f.Scan() {
            currentRecipe = Line(f.Text(), currentRecipe, nutriments, total)
    }
    fmt.Println(total)
}

func Line (line, currentRecipe string, nut map[string][]string, tot map[string]map[string]bool) string {
    if foundrec, _ := regexp.MatchString(&quot;^# [[:graph:]]+&quot;, line); foundrec { // Begin recipe
            currentRecipe = line[2:]
            fmt.Println(&quot;---------&quot;, currentRecipe) // update current
            tot[currentRecipe] = make(map[string]bool) // prepare set
    }
    if foundnut, _ := regexp.MatchString(&quot;^- [[:graph:]]+&quot;, line); foundnut { // Begin ingredients list
            nutri := line[2:]
            tot[currentRecipe][nutri] = true
    }
    return currentRecipe
}

huangapple
  • 本文由 发表于 2021年8月24日 23:34:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/68910152.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定