集合映射的初始化(Go)

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

map of sets initialization (Go)

问题

以下是翻译好的内容:

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

  1. # applepie
  2. - 面粉
  3. - 苹果
  4. - 鸡蛋
  5. # pizza
  6. - 面粉
  7. - 奶酪
  8. - 鸡蛋
  9. - 番茄

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

但是为什么呢?每个“子映射”在 (1) 处都使用 make 进行了初始化?

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "regexp"
  7. )
  8. func main() {
  9. nutriments := map[string][]string{
  10. "flour": {"sugars"},
  11. "egg": {"protein", "fat"},
  12. "tomato": {"water", "viamins"},
  13. "cheese": {"calcium", "protein"},
  14. "apple": {"sugars", "fiber", "vitamin"},
  15. }
  16. total := make(map[string]map[string]bool)
  17. f := bufio.NewScanner(os.Stdin)
  18. currentRecipe := ""
  19. for f.Scan() {
  20. Line(f.Text(), currentRecipe, nutriments, total)
  21. }
  22. fmt.Println(total)
  23. }
  24. func Line(line, currentRecipe string, nut map[string][]string, tot map[string]map[string]bool) {
  25. if foundrec, _ := regexp.MatchString("^# [[:graph:]]+", line); foundrec { // 开始食谱
  26. currentRecipe := line[2:]
  27. fmt.Println("---------", currentRecipe) // 更新当前食谱
  28. tot[currentRecipe] = make(map[string]bool) // <== 初始化集合 (1)
  29. }
  30. if foundnut, _ := regexp.MatchString("^- [[:graph:]]+", line); foundnut { // 新成分
  31. nutri := line[2:]
  32. tot[currentRecipe][nutri] = true // <===== 这里 (2)
  33. }
  34. }
英文:

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

  1. # applepie
  2. - flour
  3. - apple
  4. - egg
  5. # pizza
  6. - flour
  7. - cheese
  8. - egg
  9. - tomato

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

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

  1. package main
  2. import (
  3. &quot;bufio&quot;
  4. &quot;fmt&quot;
  5. &quot;os&quot;
  6. &quot;regexp&quot;
  7. )
  8. func main() {
  9. nutriments := map[string][]string{
  10. &quot;flour&quot;:{&quot;sugars&quot;},
  11. &quot;egg&quot;:{&quot;protein&quot;, &quot;fat&quot;},
  12. &quot;tomato&quot;:{&quot;water&quot;, &quot;viamins&quot;},
  13. &quot;cheese&quot;:{&quot;calcium&quot;, &quot;protein&quot;},
  14. &quot;apple&quot;:{&quot;sugars&quot;, &quot;fiber&quot;, &quot;vitamin&quot;} }
  15. total := make(map[string]map[string]bool)
  16. f := bufio.NewScanner(os.Stdin)
  17. currentRecipe := &quot;&quot;
  18. for f.Scan() {
  19. Line(f.Text(), currentRecipe, nutriments, total)
  20. }
  21. fmt.Println(total)
  22. }
  23. func Line (line, currentRecipe string, nut map[string][]string, tot map[string]map[string]bool) {
  24. if foundrec, _ := regexp.MatchString(&quot;^# [[:graph:]]+&quot;, line); foundrec { // Begin recipe
  25. currentRecipe := line[2:]
  26. fmt.Println(&quot;---------&quot;, currentRecipe) // update current
  27. tot[currentRecipe] = make(map[string]bool) // &lt;== INITIALIZE SET (1)
  28. }
  29. if foundnut, _ := regexp.MatchString(&quot;^- [[:graph:]]+&quot;, line); foundnut { // New ingredient
  30. nutri := line[2:]
  31. tot[currentRecipe][nutri] = true // &lt;===== HERE (2)
  32. }
  33. }

答案1

得分: 1

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

以下是正确的代码:

  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "regexp"
  7. )
  8. func main() {
  9. nutriments := map[string][]string{
  10. "flour": {"sugars"},
  11. "egg": {"protein", "fat"},
  12. "tomato": {"water", "viamins"},
  13. "cheese": {"calcium", "protein"},
  14. "apple": {"sugars", "fiber", "vitamin"},
  15. }
  16. total := make(map[string]map[string]bool)
  17. f := bufio.NewScanner(os.Stdin)
  18. var currentRecipe string
  19. for f.Scan() {
  20. currentRecipe = Line(f.Text(), currentRecipe, nutriments, total)
  21. }
  22. fmt.Println(total)
  23. }
  24. func Line(line, currentRecipe string, nut map[string][]string, tot map[string]map[string]bool) string {
  25. if foundrec, _ := regexp.MatchString("^# [[:graph:]]+", line); foundrec { // Begin recipe
  26. currentRecipe = line[2:]
  27. fmt.Println("---------", currentRecipe) // update current
  28. tot[currentRecipe] = make(map[string]bool) // prepare set
  29. }
  30. if foundnut, _ := regexp.MatchString("^- [[:graph:]]+", line); foundnut { // Begin ingredients list
  31. nutri := line[2:]
  32. tot[currentRecipe][nutri] = true
  33. }
  34. return currentRecipe
  35. }

希望对你有帮助!

英文:

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

Here is correct code

  1. package main
  2. import (
  3. &quot;bufio&quot;
  4. &quot;fmt&quot;
  5. &quot;os&quot;
  6. &quot;regexp&quot;
  7. )
  8. func main() {
  9. nutriments := map[string][]string{
  10. &quot;flour&quot;:{&quot;sugars&quot;},
  11. &quot;egg&quot;:{&quot;protein&quot;, &quot;fat&quot;},
  12. &quot;tomato&quot;:{&quot;water&quot;, &quot;viamins&quot;},
  13. &quot;cheese&quot;:{&quot;calcium&quot;, &quot;protein&quot;},
  14. &quot;apple&quot;:{&quot;sugars&quot;, &quot;fiber&quot;, &quot;vitamin&quot;} }
  15. total := make(map[string]map[string]bool)
  16. f := bufio.NewScanner(os.Stdin)
  17. currentRecipe := &quot;&quot;
  18. for f.Scan() {
  19. currentRecipe = Line(f.Text(), currentRecipe, nutriments, total)
  20. }
  21. fmt.Println(total)
  22. }
  23. func Line (line, currentRecipe string, nut map[string][]string, tot map[string]map[string]bool) string {
  24. if foundrec, _ := regexp.MatchString(&quot;^# [[:graph:]]+&quot;, line); foundrec { // Begin recipe
  25. currentRecipe = line[2:]
  26. fmt.Println(&quot;---------&quot;, currentRecipe) // update current
  27. tot[currentRecipe] = make(map[string]bool) // prepare set
  28. }
  29. if foundnut, _ := regexp.MatchString(&quot;^- [[:graph:]]+&quot;, line); foundnut { // Begin ingredients list
  30. nutri := line[2:]
  31. tot[currentRecipe][nutri] = true
  32. }
  33. return currentRecipe
  34. }

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:

确定