func CanWeRead(ch <-chanint) {
select {
case <-ch:
… // yes, we can read
}
}
読み込み
ポイント:defaultがないとブ
ロックするよ!
15.
func CanWeRead(ch <-chanint) {
select {
case <-ch:
… // yes, we can read
default:
… // no, we could not read
}
}
読み込み
16.
func CanWeRead(ch <-chanint) {
select {
case <-ch:
… // yes, we can read
default:
… // no, we could not read
}
}
読み込み
ポイント:読み込みが即時でき
ない場合はデフォルトが実行さ
れるので、実質的なノンブロッ
キングI/O
17.
func CanWeRead(ch <-chanint) {
select {
case v, ok := <-ch:
if !ok { // channel is closed
…
}
fmt.Println(v)
}
}
値を使う
18.
func CanWeRead(ch <-chanint) {
select {
case v, ok := <-ch:
if !ok { // channel is closed
…
}
fmt.Println(v)
}
}
値を使う
ポイント:代入もできるよ!
19.
func CanWeWrite(ch chanint) {
select {
case ch<-1:
… // yes, we can write
default:
… // no, we could not write
}
}
書き込み
20.
func CanWeWrite(ch chanint) {
select {
case ch<-1:
… // yes, we can write
default:
… // no, we could not write
}
}
書き込み
ポイント:書き込みが即時でき
ない場合はデフォルトが実行さ
れるので、実質的なノンブロッ
キングI/O