先定义通知结构和接口,再实现邮件、控制台等多渠道发送。使用SMTP发送邮件,通过Notifier接口统一调用,结合time.Ticker实现定时提醒,支持扩展短信、Webhook等,系统简洁可扩展。

用Golang开发一个基础的通知提醒系统,核心在于实现消息的生成、分发和多种通知渠道的集成。系统不需要一开始就复杂,可以从简单的结构入手,逐步扩展。下面是一个清晰的实现思路和代码示例。
先定义通知的基本数据结构和发送行为的统一接口,便于后续扩展不同类型的发送方式。
type Notification struct {
Title string
Content string
To string // 邮箱、手机号等接收目标
}
<p>type Notifier interface {
Send(n Notification) error
}</p>这样设计可以支持多种通知方式(如邮件、短信、站内信)通过统一接口调用。
使用标准库 net/smtp 发送邮件是最常见的需求之一。
立即学习“go语言免费学习笔记(深入)”;
import (
"fmt"
"net/smtp"
)
<p>type EmailNotifier struct {
Auth smtp.Auth
Addr string
From string
}</p><p>func NewEmailNotifier(host, port, user, password string) *EmailNotifier {
auth := smtp.PlainAuth("", user, password, host)
addr := fmt.Sprintf("%s:%s", host, port)
return &EmailNotifier{
Auth: auth,
Addr: addr,
From: user,
}
}</p><p>func (e *EmailNotifier) Send(n Notification) error {
msg := fmt.Sprintf("To: %s\r\nSubject: %s\r\n\r\n%s", n.To, n.Title, n.Content)
return smtp.SendMail(e.Addr, e.Auth, e.From, []string{n.To}, []byte(msg))
}</p>调用时只需创建实例并传入通知对象:
notifier := NewEmailNotifier("smtp.gmail.com", "587", "you@gmail.com", "password")
err := notifier.Send(Notification{
Title: "系统提醒",
Content: "您的任务已超期。",
To: "user@example.com",
})
if err != nil {
fmt.Println("发送失败:", err)
}
</font><H3>添加日志或控制台通知(用于调试)</H3><p>在开发阶段或作为备用通道,打印到控制台也很有用。</p>
<div class="aritcle_card">
<a class="aritcle_card_img" href="/ai/%E5%A6%82%E7%9F%A5ai%E7%AC%94%E8%AE%B0">
<img src="https://img.php.cn/upload/ai_manual/000/000/000/175679994166405.png" alt="如知AI笔记">
</a>
<div class="aritcle_card_info">
<a href="/ai/%E5%A6%82%E7%9F%A5ai%E7%AC%94%E8%AE%B0">如知AI笔记</a>
<p>如知笔记——支持markdown的在线笔记,支持ai智能写作、AI搜索,支持DeepseekR1满血大模型</p>
<div class="">
<img src="/static/images/card_xiazai.png" alt="如知AI笔记">
<span>27</span>
</div>
</div>
<a href="/ai/%E5%A6%82%E7%9F%A5ai%E7%AC%94%E8%AE%B0" class="aritcle_card_btn">
<span>查看详情</span>
<img src="/static/images/cardxiayige-3.png" alt="如知AI笔记">
</a>
</div>
<font color="#0066cc"><pre class="brush:php;toolbar:false;">
type ConsoleNotifier struct{}
<p>func (c *ConsoleNotifier) Send(n Notification) error {
fmt.Printf("[通知] 发送给 %s: %s - %s\n", n.To, n.Title, n.Content)
return nil
}</p>你可以将多个通知器组合使用:
func SendToAll(notifiers []Notifier, n Notification) {
for _, notifier := range notifiers {
_ = notifier.Send(n) // 忽略错误或记录日志
}
}
很多提醒是周期性或延迟触发的,可以用 time.Ticker 或 time.AfterFunc 实现。
func ScheduleReminder(intervalSec int, notifier Notifier, notification Notification) {
ticker := time.NewTicker(time.Duration(intervalSec) * time.Second)
go func() {
for range ticker.C {
notifier.Send(notification)
}
}()
}
比如每30秒提醒一次:
ScheduleReminder(30, &ConsoleNotifier{}, Notification{
Title: "健康检查提醒",
Content: "请检查服务状态。",
To: "admin",
})
基本上就这些。通过结构体+接口的方式,你可以轻松添加短信(接入第三方API)、Webhook、WebSocket推送等更多方式。系统保持简单、可测试、可扩展,适合中小型项目的基础提醒需求。
以上就是Golang如何开发基础的通知提醒系统的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号