Last active
February 26, 2018 14:51
-
-
Save syossan27/50d69558aa3ee46eb73d3fedb279155c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func main() { | |
| // シグナルを受け取るまで待ち受けるためのChannel | |
| doneCh := make(chan struct{} | |
| // シグナルを受け取るChannel | |
| signalCh := make(chan os.Signal, 1) | |
| signal.Notify(signalCh, os.Interrupt) | |
| // メッセージを受け取る処理をgoroutineで動かす | |
| go receive(signalCh, doneCh) | |
| // シグナルを受け取るまで待ち受ける | |
| <-doneCh | |
| } | |
| func receive(signalCh chan os.Signal, doneCh chan struct{}) { | |
| for { | |
| select { | |
| // case msg := <-hoge(): メッセージを受け取って何か処理を行なう | |
| case <-signalCh: | |
| doneCh <- struct{}{} | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment