我有一个我们称之为 Tpus 的公共变量,我在无限循环中修改了这个值,但是当我尝试在 for 循环中打印 Tpus 以及在 for 循环外的 goroutine 中打印 Tpus 时,我得到了 2 个不同的结果,我在寻找什么做的是在 goroutine 中得到与我在 for 循环中得到的结果相同的结果。
var Tpus []string
func TPU_Calc(destination string) {
clusternodes := GetClusterNodes(destination)
go func() {
wg.Add(1)
time.Sleep(2 * time.Second)
for {
time.Sleep(150 * time.Millisecond)
//fmt.Printf("\n + %e", Tpus)
}
}()
for {
slot := GetSlot(destination)
slotleaders := GetSlotLeaders(destination, strconv.FormatUint(slot+10, 10))
for x := 0; x < 80; x++ {
for z := 0; z < len(clusternodes.Result); z++ {
if slotleaders[x] == clusternodes.Result[z].Pubkey {
if len(Tpus) >= 2 {
X := RemoveIndex(Tpus, 0)
Tpus = append(X, clusternodes.Result[x].Tpu)
fmt.Printf("\n + %e", Tpus)
} else {
Tpus = append(Tpus, clusternodes.Result[x].Tpu)
}
}
}
}
}
}
上面代码的结果:
[%!e(字符串=136.144.49.213:8004)%!e(字符串=198.55.56.164:8004)]
[%!e(字符串=198.55.56.164:8004)%!e(字符串=13.124.174.97:8003)]
[%!e(字符串=13.124.174.97:8003) %!e(字符串=185.16.38.73:8003)]
[%!e(字符串=185.16.38.73:8003) %!e(字符串=103.28.52.250:8003)]
[%!e(字符串=103.28.52.250:8003) %!e(字符串=18.214.103.198:8004)]
[%!e(字符串=18.214.103.198:8004)%!e(字符串=185.188.42.43:9003)]
[%!e(字符串=185.188.42.43:9003)%!e(字符串=135.181.115.253:8003)]
UYOU
慕尼黑的夜晚无繁华
随时随地看视频慕课网APP
相关分类