bug caused by for iteration in go

Here is the code:

    for _, cn := range checked {
            if cn.Status.Phase == v1alpha1.LksnodeVMReady {
                    go m.deployK8sNode(lc, &cn)
            }
    }

We find that in for iteration, the cn variable reference is not changed. Suppose we have two cns in checked, this means that when the second go thread starts with cn set to the second object, the first thread’s cn will also changes, which ends up into mess. The solution is to pass a blank new cn to each thread.

    for _, cn := range checked {
            if cn.Status.Phase == v1alpha1.LksnodeVMReady {
                    newcn := cn
                    go m.deployK8sNode(lc, &newcn)
            }
    }

添加好友我们可以聊更多相关话题二维码