Send desktop notifications in Go

0
2862
Send desktop notifications in Go
Send desktop notifications in Go

In this article, I will show you how to send desktop notifications in Go (golang). You can also make alerts and beep sound.

First, create a Go project and add following dependency into the project:

$ go get -u github.com/gen2brain/beeep

The beeep library provides a cross-platform library for sending desktop notifications, alerts and beeps.

To send a beep:

// send a beep

func execBeep() {
	err := beeep.Beep(beeep.DefaultFreq, beeep.DefaultDuration)
	if err != nil {
		panic(err)
	}
}

To send notification:

// send notification

func execNotify() {
	err := beeep.Notify("Hello", "Nice to meet you", "info.png")
	if err != nil {
		panic(err)
	}
}

To send an alert:

// send alert

func execAlert() {
	err := beeep.Alert("Alert!", "Timeout! You need to take a break!", "warning.png")
	if err != nil {
		panic(err)
	}
}
NOTE

The 3rd parameter of Notify() and Alert() is not applicable on MacOS, instead, it will use default Script Editor icon. Therefore, you can pass an empty string.

That’s it, now you know how to send desktop notifications in Go, right?

Have fun!