-1

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 { ... }
Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
steveyang
  • 9,178
  • 8
  • 54
  • 80

1 Answers1

3

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

nobody
  • 46
  • 1