A MacOS sleep notifier for Go. Provides a simple way to detect when a machine goes to sleep.
Heavily inspired by https://github.com/prashantgupta24/mac-sleep-notifier, however:
- has a safe way to subscribe multiple listeners,
- a way to unsubscribe,
- supports context to stop listening
ctx, cancel:=context.WithCancel(context.Background())
defercancel()
ctx, stop:=signal.NotifyContext(ctx, os.Interrupt, os.Kill)
deferstop()
gofunc() {
forn:=rangesleeper.Subscribe(ctx) {
slog.Info("New notification", "state", string(n.Type))
}
}()
sleeper.Await()Working example in a example/main.go file.
go run example/main.go