gobyexample/signals/signals.go

31 lines
495 B
Go
Raw Permalink Normal View History

2024-05-04 18:32:38 +00:00
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
}