バッファありチャネルとバッファなしチャネル
先日、ゴルーチンとチャネルを使った以下のようなプログラムを作成したところ、うまく動作しませんでした。
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 29 30 31 32 |
// 動作しない例 package main import ( "fmt" ) func testFunc(sendCh <-chan struct{}) { for { select { case <-sendCh: fmt.Println("Received") default: // Do nothing. } } } func main() { sendCh := make(chan struct{}) // バッファなしチャネル go testFunc(sendCh) for { select { case sendCh <- struct{}{}: fmt.Println("Send") default: // Do nothing. } } } |
メインのゴルーチンがチャネルに送信して、もうひとつの… バッファありチャネルとバッファなしチャネル の続きを読む