go-notificationgo-notification
Getting Started

Quick Start

Send your first notification in five minutes.

This walks through sending an email via Mailgun — but the same pattern applies to every channel.

1. Create the notifier

main.go
notifier := notification.New(notification.Config{
    // defaults: async worker pool, retries, exponential backoff
})

2. Register a channel

Each channel has a name (your choice) and a driver (a concrete implementation).

main.go
notifier.RegisterChannel("mail", mailgun.New(mailgun.Config{
    Domain: "mg.example.com",
    APIKey: os.Getenv("MAILGUN_API_KEY"),
    From:   "noreply@example.com",
}))

You can register the same driver under multiple names (e.g. mail-transactional and mail-marketing) with different configs.

3. Implement Notifiable on your user type

Notifiable tells go-notification how to reach a given recipient:

main.go
type User struct {
    ID    int64
    Name  string
    Email string
}

func (u User) RouteNotificationFor(channel string) any {
    switch channel {
    case "mail":
        return u.Email
    }
    return nil
}

4. Define a notification

A notification is any struct that implements Notification:

main.go
type OrderShipped struct {
    OrderID string
}

func (n OrderShipped) Via(notifiable notification.Notifiable) []string {
    return []string{"mail"}
}

func (n OrderShipped) ToMail(notifiable notification.Notifiable) *mail.Message {
    return mail.NewMessage().
        Subject("Your order has shipped").
        Line("Hi, your order " + n.OrderID + " is on its way.").
        Action("Track package", "https://example.com/track/"+n.OrderID)
}

5. Send it

main.go
user := User{ID: 1, Name: "Dani", Email: "dani@example.com"}
notifier.Send(ctx, user, OrderShipped{OrderID: "A-1024"})

That's the end-to-end loop. Send() returns immediately; dispatch runs on the worker pool.