go 方法
2017-11-02 本文已影响4人
AEGQ
Go 既允许使用值,也允许使用指针来调用方法,不必严格符合接收者的类型。
package main
import (
"fmt"
)
type user struct {
name string
email string
}
func (u user) notify() {
fmt.Printf("Sending: %s -> %s \n", u.name, u.email)
}
func (u *user) change(email string) {
u.email = email
}
func main() {
bill := user{"Bill", "bill@email.com"}
bill.notify()
lisa := &user{"lisa", "lisa@email.com"}
lisa.notify()
bill.change("bill@new.com")
bill.notify()
lisa.change("lisa@new.com")
lisa.notify()
}
Sending: Bill -> bill@email.com
Sending: lisa -> lisa@email.com
Sending: Bill -> bill@new.com
Sending: lisa -> lisa@new.com