Golang 创建一个名为Mailer的接口,定义了一个Send方法
在本文中,我们将创建一个名为mailer的接口,使用接口嵌入和函数作为参数来定义一个send方法。Go语言中的接口是一组定义了一组行为的方法。
步骤
创建一个Mailer接口,它有一个接受两个参数的Send函数:收件人的电子邮件地址和电子邮件正文。如果在发送过程中发生错误,Send方法应返回一个错误。
示例1
- 创建表示各种邮件发送器实现的结构,如SmtpMailer、SendGridMailer等等。
-
根据各个邮件发送器实现的邮件发送逻辑实现Send函数。
-
创建一个名为MailerFacade的新结构,它具有Mailer接口。
-
这种类型作为容器,用于存放多个邮件发送器的实例。
-
在主程序中创建邮件发送器实现的实例,并将它们封装在MailerFacade实例中。
-
将MailerFacade实例保存在一个Mailer接口切片中。遍历切片,在每个邮件发送器上调用Send方法,参数为收件人的电子邮件地址和电子邮件正文。
示例2
- 创建描述各种电子邮件发送或写入逻辑的函数,如SendSMTPMail、SendSendGridMail等等。
-
检查这些方法是否具有与Mailer接口的Send方法相同的签名。
-
在主程序中创建一个函数切片,每个函数都具有与Send方法相同的签名。
-
用反映各种电子邮件发送或写入逻辑的函数填充切片。
-
遍历切片,调用每个函数,参数为收件人的电子邮件地址和电子邮件正文。
示例1
他的示例允许接口嵌入其他接口,使嵌入接口继承嵌入接口的所有方法。下面的代码定义了满足mailer接口的多个mailer实现。main函数创建了邮件发送器的实例,并用一个迭代器包装了一个mailer函数切片来发送邮件。
package main
import "fmt"
type Mailer interface {
Send(string, string) error
}
type SmtpMailer struct{}
func (s *SmtpMailer) Send(to, body string) error {
fmt.Printf("Sending SMTP mail to: %s\nBody: %s\n", to, body)
return nil
}
type SendGridMailer struct{}
func (s *SendGridMailer) Send(to, body string) error {
fmt.Printf("Sending SendGrid mail to: %s\nBody: %s\n", to, body)
return nil
}
type FileMailer struct{}
func (f *FileMailer) Send(to, body string) error {
fmt.Printf("Writing mail to file for: %s\nBody: %s\n", to, body)
return nil
}
type MailerFacade struct {
Mailer
}
func main() {
smtpMailer := &SmtpMailer{}
sendGridMailer := &SendGridMailer{}
fileMailer := &FileMailer{}
smtpMailerFacade := MailerFacade{smtpMailer}
sendGridMailerFacade := MailerFacade{sendGridMailer}
fileMailerFacade := MailerFacade{fileMailer}
mailers := []Mailer{&smtpMailerFacade, &sendGridMailerFacade, &
fileMailerFacade}
for _, mailer := range mailers {
err := mailer.Send("emil@example.com", "Hello, World!")
if err != nil {
fmt.Printf("Error sending mail: %v\n", err)
}
}
}
输出
Sending SMTP mail to: email@example.com
Body: Hello, World!
Sending SendGrid mail to: email@example.com
Body: Hello, World!
Writing mail to file for: email@example.com
Body: Hello, World!
示例2
在这个示例中,我们将使用Go语言中的切片函数来选择不同的邮件发送实现。通过这种方法,我们可以轻松地在不同的邮件发送技术之间进行切换。下面的代码定义了表示不同邮件发送程序的多个函数,以及一个主函数,该函数迭代一个邮件发送程序的切片,并调用每个函数将发送的邮件写入文件中。
package main
import "fmt"
type Mailer interface {
Send(to, body string) error
}
func SendSMTPMail(to, body string) error {
fmt.Printf("Sending SMTP mail to: %s\nBody: %s\n", to, body)
return nil
}
func SendSendGridMail(to, body string) error {
fmt.Printf("Sending SendGrid mail to: %s\nBody: %s\n", to, body)
return nil
}
func WriteMailToFile(to, body string) error {
fmt.Printf("Writing mail to file for: %s\nBody: %s\n", to, body)
return nil
}
func main() {
mailers := []func(string, string) error{
SendSMTPMail,
SendSendGridMail,
WriteMailToFile,
}
for _, mailer := range mailers {
err := mailer("email@example.com", "Hello, World!")
if err != nil {
fmt.Printf("Error sending mail: %v\n", err)
}
}
}
输出
Sending SMTP mail to: email@example.com
Body: Hello, World!
Sending SendGrid mail to: email@example.com
Body: Hello, World!
Writing mail to file for: email@example.com
Body: Hello, World!
结论
在本文中,我们讨论了如何创建一个名为mailer的接口,该接口定义了一个send方法,通过使用接口,我们为不同的mailer实现定义了一个公共契约。在这里我们讨论了两种方法,包括嵌入接口和一个主要方法。