英文:
Map overwrite in a switch statement
问题
我正在处理一个项目,使用GO语言,但是遇到了一个小问题。我将在下面放置代码,然后解释我在理解发生了什么方面遇到了困难的地方:
package main
import (
"fmt"
)
type Population struct {
cellNumber map[int]Cell
}
type Cell struct {
cellState string
cellRate int
}
var (
envMap map[int]Population
stemPopulation Population
taPopulation Population
)
func main() {
envSetup := make(map[string]int)
envSetup["SC"] = 1
envSetup["TA"] = 1
initialiseEnvironment(envSetup)
}
func initialiseEnvironment(envSetup map[string]int) {
cellMap := make(map[int]Cell)
for cellType := range envSetup {
switch cellType {
case "SC":
{
for i := 0; i <= envSetup[cellType]; i++ {
cellMap[i] = Cell{"active", 1}
}
stemPopulation = Population{cellMap}
}
case "TA":
{
for i := 0; i <= envSetup[cellType]; i++ {
cellMap[i] = Cell{"juvenille", 2}
}
taPopulation = Population{cellMap}
}
default:
fmt.Println("Default case does nothing!")
}
fmt.Println("The Stem Cell Population: \n", stemPopulation)
fmt.Println("The TA Cell Population: \n", taPopulation)
fmt.Println("\n")
}
}
我遇到的问题是,当我们到达switch语句中的"TA"情况时,stemPopulation的内容被taPopulation覆盖。
我在for循环中显式地放置了打印语句以查看发生了什么:
For循环步骤1:
The Stem Cell Population:
{map[0:{active 1} 1:{active 1}]}
The TA Cell Population:
{map[]}
For循环步骤2:
The Stem Cell Population:
{map[0:{juvenille 2} 1:{juvenille 2}]}
The TA Cell Population:
{map[0:{juvenille 2} 1:{juvenille 2}]}
我期望的是:
For循环步骤1:
The Stem Cell Population:
{map[0:{active 1} 1:{active 1}]}
The TA Cell Population:
{map[]}
For循环步骤2:
The Stem Cell Population:
{map[0:{active 1} 1:{active 1}]}
The TA Cell Population:
{map[0:{juvenile 2} 1:{juvenile 2}]}
有人可以帮助我理解发生了什么以及为什么会发生这种情况吗?这是因为我在开头声明的全局变量吗?还是我犯了一个代码错误?
英文:
I'm working on a project and I am using GO, but I have hit a small obstacle. I will put the code below and then explain where I'm having trouble understanding what is happening:
package main
import (
"fmt"
)
type Population struct {
cellNumber map[int]Cell
}
type Cell struct {
cellState string
cellRate int
}
var (
envMap map[int]Population
stemPopulation Population
taPopulation Population
)
func main() {
envSetup := make(map[string]int)
envSetup["SC"] = 1
envSetup["TA"] = 1
initialiseEnvironment(envSetup)
}
func initialiseEnvironment(envSetup map[string]int) {
cellMap := make(map[int]Cell)
for cellType := range envSetup {
switch cellType {
case "SC":
{
for i := 0; i <= envSetup[cellType]; i++ {
cellMap[i] = Cell{"active", 1}
}
stemPopulation = Population{cellMap}
}
case "TA":
{
for i := 0; i <= envSetup[cellType]; i++ {
cellMap[i] = Cell{"juvenille", 2}
}
taPopulation = Population{cellMap}
}
default:
fmt.Println("Default case does nothing!")
}
fmt.Println("The Stem Cell Population: \n", stemPopulation)
fmt.Println("The TA Cell Population: \n", taPopulation)
fmt.Println("\n")
}
}
The issue that I'm having is that the content of stemPopulation is being overwritten by taPopulation when we arrive at the "TA" case in the switch statement.
I explicitly put the print statements in the for loop to see what is going on:
For-loop Step1:
The Stem Cell Population:
{map[0:{active 1} 1:{active 1}]}
The TA Cell Population:
{map[]}
For-loop Step2:
The Stem Cell Population:
{map[0:{juvenille 2} 1:{juvenille 2}]}
The TA Cell Population:
{map[0:{juvenille 2} 1:{juvenille 2}]}
What I am expecting:
For-loop Step1:
The Stem Cell Population:
{map[0:{active 1} 1:{active 1}]}
The TA Cell Population:
{map[]}
For-loop Step2:
The Stem Cell Population:
{map[0:{active 1} 1:{active 1}]}
The TA Cell Population:
{map[0:{juvenile 2} 1:{juvenile 2}]}
Could anybody help me understand what is going on and why it is happening ? Is it because of the global variables that I declared at the beginning? or is it a code error that I made?
答案1
得分: 3
这两个结构共享相同的cellMap
。将cellMap
的创建移到循环内部,你的代码就可以正常工作了。
英文:
The two structures share the same cellMap
. Move the creation of cellMap into the loop and your code will work.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论