在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
// Sample program to show how to declare methods and how the Go // compiler supports them. package main import ( "fmt" ) // user defines a user in the program. type user struct { name string email string } // notify implements a method with a value receiver. func (u user) notify() { fmt.Printf("Sending User Email To %s<%s>\n", u.name, u.email) } // changeEmail implements a method with a pointer receiver. func (u *user) changeEmail(email string) { fmt.Printf("change Email from <%s> To <%s>\n", u.email, email) u.email = email } // main is the entry point for the application. func main() { // Values of type user can be used to call methods // declared with a value receiver. bill := user{"Bill", "[email protected]"} bill.notify() // Pointers of type user can also be used to call methods // declared with a value receiver. lisa := &user{"Lisa", "[email protected]"} lisa.notify() fmt.Println() // Values of type user can be used to call methods // declared with a pointer receiver. bill.changeEmail("[email protected]") bill.notify() // Pointers of type user can be used to call methods // declared with a pointer receiver. lisa.changeEmail("[email protected]") lisa.notify() } 输出 Sending User Email To Bill<[email protected]> Sending User Email To Lisa<[email protected]> change Email from <[email protected]> To <[email protected]> Sending User Email To Bill<[email protected]> change Email from <[email protected]> To <[email protected]> Sending User Email To Lisa<[email protected]>
|
请发表评论