猿问

访问在 for 循环内修改的公共变量时,我看不到变化

我有一个我们称之为 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
浏览 140回答 1
1回答

慕尼黑的夜晚无繁华

var globalvar []stringfunc main() {&nbsp; &nbsp; var mu sync.Mutex&nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Block access to a global variable, so that&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // no one can change it outside the goroutine.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // If it's already locked outside the goroutine,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // then wait for unlocking.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mu.Lock()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Some actions with a global variable...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("%v\n", globalvar)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Unlocking access to a global variable&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mu.Unlock()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Some code...&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }()&nbsp; &nbsp; for i := 0; i < 255; i++ {&nbsp; &nbsp; &nbsp; &nbsp; // Block access to a global variable.&nbsp; &nbsp; &nbsp; &nbsp; // If it's already locked inside the goroutine,&nbsp; &nbsp; &nbsp; &nbsp; // then wait for unlocking.&nbsp; &nbsp; &nbsp; &nbsp; mu.Lock()&nbsp; &nbsp; &nbsp; &nbsp; // Some actions with a global variable&nbsp; &nbsp; &nbsp; &nbsp; globalvar = append(globalvar, "Str #"+strconv.Itoa(i))&nbsp; &nbsp; &nbsp; &nbsp; // Unlock access&nbsp; &nbsp; &nbsp; &nbsp; mu.Unlock()&nbsp; &nbsp; &nbsp; &nbsp; // Some code...&nbsp; &nbsp; }}您还可以定义一个特殊的结构,其中包含互斥体、值和更改其值的方法。是这样的:type TpusContainer struct {&nbsp; &nbsp; mu&nbsp; &nbsp; sync.Mutex&nbsp; &nbsp; value []string}func (t *TpusContainer) RemoveIndex(i int) {&nbsp; &nbsp; t.mu.Lock()&nbsp; &nbsp; defer t.mu.Unlock()&nbsp; &nbsp; t.value = RemoveIndex(t.value, i)}func (t *TpusContainer) Append(elem string) {&nbsp; &nbsp; t.mu.Lock()&nbsp; &nbsp; defer t.mu.Unlock()&nbsp; &nbsp; t.value = append(t.value, elem)}func (t *TpusContainer) String() string {&nbsp; &nbsp; t.mu.Lock()&nbsp; &nbsp; defer t.mu.Unlock()&nbsp; &nbsp; return fmt.Sprintf("%v", t.value)}var Tpus TpusContainerfunc main() {&nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("%v\n", Tpus)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }()&nbsp; &nbsp; for i := 0; i < 255; i++ {&nbsp; &nbsp; &nbsp; &nbsp; Tpus.RemoveIndex(0)&nbsp; &nbsp; &nbsp; &nbsp; Tpus.Append("Some string #"+strconv.Itoa(i))&nbsp; &nbsp; }}就个人而言,我更喜欢第二种方法。
随时随地看视频慕课网APP

相关分类

Go
我要回答