Skip to content

Instantly share code, notes, and snippets.

@syossan27
Last active February 26, 2018 14:51
Show Gist options
  • Select an option

  • Save syossan27/50d69558aa3ee46eb73d3fedb279155c to your computer and use it in GitHub Desktop.

Select an option

Save syossan27/50d69558aa3ee46eb73d3fedb279155c to your computer and use it in GitHub Desktop.
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