31 lines
495 B
Go
31 lines
495 B
Go
package signals
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
)
|
|
|
|
func Signals() {
|
|
// chan MUST be buffered
|
|
signals := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
done := make(chan bool, 1)
|
|
|
|
go doWork(done, signals)
|
|
|
|
fmt.Println("Awaiting signal")
|
|
<-done
|
|
fmt.Println("Received done, program exiting")
|
|
}
|
|
|
|
func doWork(done chan<- bool, signals <-chan os.Signal) {
|
|
sig := <-signals
|
|
fmt.Println()
|
|
fmt.Println("Received signal:", sig)
|
|
done <- true
|
|
}
|