Inspired from this blog.
This libary notifies through a channel whenever your machine is put to sleep or is woken up. Calling the Start
function will get you a channel on which you receive both Sleep
and Awake
activities.
go get -u github.com/prashantgupta24/mac-sleep-notifier/notifier
notifierCh := notifier.GetInstance().Start()
for {
select {
case activity := <-notifierCh:
if activity.Type == notifier.Awake {
log.Println("machine awake")
} else {
if activity.Type == notifier.Sleep {
log.Println("machine sleeping")
}
}
}
}
See example here