Я работаю над проектом, и я использую 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")
}
}
Вопрос, который я имею, что содержание stemPopulation в перезаписи taPopulation когда мы приходим к случаю «TA» в инструкции switch.
я явно поставил операторы печати в цикл, чтобы увидеть, что происходит:
Для контура Шаг1:
The Stem Cell Population:
{map[0:{active 1} 1:{active 1}]}
The TA Cell Population:
{map[]}
Для контура Step2:
The Stem Cell Population:
{map[0:{juvenille 2} 1:{juvenille 2}]}
The TA Cell Population:
{map[0:{juvenille 2} 1:{juvenille 2}]}
Что Я ожидаю:
For-loop Step1:
The Stem Cell Population:
{map[0:{active 1} 1:{active 1}]}
The TA Cell Population:
{map[]}
Для контура Step2:
The Stem Cell Population:
{map[0:{active 1} 1:{active 1}]}
The TA Cell Population:
{map[0:{juvenile 2} 1:{juvenile 2}]}
Может кто-нибудь помочь мне понять, что происходит и почему это происходит? Это из-за глобальных переменных, которые я объявил в начале? или это ошибка кода, которую я сделал?
спасибо !! Теперь имеет смысл, что я думаю об этом ... Я должен быть более осторожным. – sSmacKk