在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
// Sample program to show how to use an interface in Go. package main import ( "fmt" ) // notifier is an interface that defined notification // type behavior. type notifier interface { notify() } // user defines a user in the program. type user struct { name string email string } // notify implements a method with a pointer receiver. func (u *user) notify() { fmt.Printf("Sending user email to %s<%s>\n", u.name, u.email) } // main is the entry point for the application. func main() { // Create a value of type User and send a notification. u := &user{"Bill", "[email protected]"} sendNotification(u) // ./listing36.go:32: cannot use u (type user) as type // notifier in argument to sendNotification: // user does not implement notifier // (notify method has pointer receiver) } // sendNotification accepts values that implement the notifier // interface and sends notifications. func sendNotification(n notifier) { n.notify() } 输出 Sending user email to Bill<[email protected]>
|
请发表评论