go 接口以及对象传递

 

// 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", "bill@email.com"}

    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<bill@email.com>

 

上一篇:Linux内核事件通知链学习


下一篇:V4L2