| 1 | const n = 1000 |
| 2 | |
| 3 | fn f(ch chan int) { |
| 4 | mut s := 0 |
| 5 | for _ in 0 .. n { |
| 6 | s += <-ch or { |
| 7 | println('Something went wrong:') |
| 8 | println('got ${err}') |
| 9 | } |
| 10 | } |
| 11 | assert s == n * (n + 1) / 2 |
| 12 | ch.close() |
| 13 | } |
| 14 | |
| 15 | fn do_send(ch chan int, val int) ?int { |
| 16 | ch <- val? |
| 17 | return val + 1 |
| 18 | } |
| 19 | |
| 20 | fn do_send_2(ch chan int, val int) ?int { |
| 21 | ch <- val or { return error('could not send') } |
| 22 | return val + 1 |
| 23 | } |
| 24 | |
| 25 | fn main() { |
| 26 | ch := chan int{} |
| 27 | spawn f(ch) |
| 28 | mut s := 1 |
| 29 | for { |
| 30 | s = do_send(ch, s) or { break } |
| 31 | } |
| 32 | assert s == n + 1 |
| 33 | } |
| 34 | |