GoroutineでもCoroutineでもないコルーチン的なもの、 「Koroutine」(造語です)を実装してみます。
KoroutineはThreadより軽量で並行動作可能な処理です。 各KoroutineやメインスレッドはChannelを通して通信します。
以下が実行例です。 スレッド 1...1 KoroutineSystem 1...2 Koroutine 1...1 Fiber の関係性で1スレッドで複数のKoroutineが実行される様子がわかります。
# Koroutine :oneが開始
Resuming for the first time
The fiber resumed at first time
# Koroutine :oneがch1待ちのため停止…したがch1に既にメッセージがあったので即座に再開
Asynchronously waiting for any message to arrive: #<Channel:0x007fde8a82bd40>
Resumed with #<Channel:0x007fde8a82bd40>
# ここで:oneから:twoへコンテキストを切り替え
# Koroutine :twoが開始
Resuming for the first time
The second fiber resumed at first time
# :twoがch2に書き込み
The second fiber wrote to ch2
The fiber should have been exited
# ここで:twoから:oneへコンテキスト切り替え
The fiber resumed with the first message: mikoto
Asynchronously waiting for any message to arrive: #<Channel:0x007fde8a82b9a8>
The fiber resumed with the second message: kuroko
The fiber should have been exited with value: #<ConditionVariable:0x007fde8a82b480>
mikotoxkuroko