@kjk1/

gobook channels non blocking wait

Go

No description

fork
loading
Files
  • main.go

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.6780181095396796","path":"main.go","file":{"path":"main.go","content":{"asEncoding":{"base64":"cGFja2FnZSBtYWluCgppbXBvcnQgKAoJImZtdCIKCSJ0aW1lIgopCgovLyA6c2hvdyBzdGFydApmdW5jIG1haW4oKSB7CgljaCA6PSBtYWtlKGNoYW4gaW50LCAxKQoKZW5kOgoJZm9yIHsKCQlzZWxlY3QgewoJCWNhc2UgbiA6PSA8LWNoOgoJCQlmbXQuUHJpbnRmKCJSZWNlaXZlZCAlZCBmcm9tIGEgY2hhbm5lbFxuIiwgbikKCQkJYnJlYWsgZW5kCgkJZGVmYXVsdDoKCQkJZm10LlByaW50KCJDaGFubmVsIGlzIGVtcHR5XG4iKQoJCQljaCA8LSA4CgkJfQoJCS8vIHdhaXQgZm9yIGNoYW5uZWwgdG8gYmUgZmlsbGVkIHdpdGggdmFsdWVzCgkJLy8gZG9uJ3QgdXNlIHRpbWUuU2xlZXAoKSBsaWtlIHRoYXQgaW4gcHJvZHVjdGlvbiBjb2RlCgkJdGltZS5TbGVlcCgyMCAqIHRpbWUuTWlsbGlzZWNvbmQpCgl9Cn0KCi8vIDpzaG93IGVuZA=="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package main

import (
	"fmt"
	"time"
)

// :show start
func main() {
	ch := make(chan int, 1)

end:
	for {
		select {
		case n := <-ch:
			fmt.Printf("Received %d from a channel\n", n)
			break end
		default:
			fmt.Print("Channel is empty\n")
			ch <- 8
		}
		// wait for channel to be filled with values
		// don't use time.Sleep() like that in production code
		time.Sleep(20 * time.Millisecond)
	}
}

// :show end