Philosophy and "for" loops — more from Go and Rust
Philosophy and "for" loops — more from Go and Rust
Posted Jul 8, 2013 4:15 UTC (Mon) by nigeltao (guest, #87676)Parent article: Philosophy and "for" loops — more from Go and Rust
for line, ok := file.readLine(); ok; line, ok = file.readLine() {
etc
}
can be re-written to respect Don't Repeat Yourself, albeit being vertically longer:
for {
line, ok := file.readLine()
if !ok {
break
}
etc
}
Second, information should always flow on a channel from sender to receiver. Closing a channel is information. If the receiver calls "close(c)" concurrently with the sender sending "c <- line" then the sender may panic, as that's what sending on a closed channel does. http://play.golang.org/p/NpnsUBWjvL
If you must have a back-channel, then literally have a second channel that points the other way, and use a select on the sender side. http://play.golang.org/p/uG_vwux0yi
The LWN site is currently under high scraper load, so comment display has been suppressed for anonymous users. If you are a human, you may read the comments by clicking the button below:
Note: you can avoid this step in the future by logging into your LWN account.
