在switch语句中进行映射覆盖

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

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 (
	&quot;fmt&quot;
)

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[&quot;SC&quot;] = 1
	envSetup[&quot;TA&quot;] = 1

	initialiseEnvironment(envSetup)
}

func initialiseEnvironment(envSetup map[string]int) {
	cellMap := make(map[int]Cell)

	for cellType := range envSetup {
		switch cellType {
		case &quot;SC&quot;:
			{
				for i := 0; i &lt;= envSetup[cellType]; i++ {
					cellMap[i] = Cell{&quot;active&quot;, 1}
				}
				stemPopulation = Population{cellMap}

			}
		case &quot;TA&quot;:
			{
				for i := 0; i &lt;= envSetup[cellType]; i++ {
					cellMap[i] = Cell{&quot;juvenille&quot;, 2}
				}

				taPopulation = Population{cellMap}
			}
		default:
			fmt.Println(&quot;Default case does nothing!&quot;)
		}
	fmt.Println(&quot;The Stem Cell Population: \n&quot;, stemPopulation)
	fmt.Println(&quot;The TA Cell Population: \n&quot;, taPopulation)
	fmt.Println(&quot;\n&quot;)
	}
}

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.

huangapple
  • 本文由 发表于 2014年2月10日 00:03:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/21661373.html
匿名

发表评论

匿名网友

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

确定