douchuanghan1344 2018-04-05 23:41
浏览 45
已采纳

以下代码段中〜once〜变量的含义是什么?

The question is about the usage once variable in the following snippet extracted from the pipe.go of the standard go library

for once := true; once || len(b) > 0; once = false {
    select {
    case p.wrCh <- b:
        nw := <-p.rdCh
        b = b[nw:]
        n += nw
    case <-p.done:
        return n, p.writeCloseError()
    }
}

My understanding is that, the loop won't terminate as long as len(b) > 0 and the loop would be executed at least once.

So why not just write

for len(b) > 0 { ... }
  • 写回答

1条回答 默认 最新

  • douzhuangxuan3268 2018-04-05 23:57
    关注

    It looks like once is being used to make a do ... while(condition); loop, which Go does not have.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?