golang中接口类型小案例
1. 在项目中实现注册成功之后,向用户发送邮件、微信提醒
package main import "fmt" type IMessage interface { send() bool } type Email struct { email string content string } func (e *Email) send() bool { fmt.Println("发送邮件提醒:", e.email, e.content) return true } type Wechat struct { wid int content string } func (w *Wechat) send() bool { fmt.Println("发送微信提醒:", w.wid, w.content) return true } func DoSomething(messageSlice []IMessage) { for _, item := range messageSlice { ret := item.send() fmt.Println(ret) } } func main() { // 在项目中实现注册成功之后,向用户发送邮件、微信提醒 messageSlice := []IMessage{ &Email{email: "1341935532@qq.com", content: "邮件提醒"}, &Wechat{wid: 110, content: "微信提醒"}, } DoSomething(messageSlice) }