码迷,mamicode.com
首页 > 编程语言 > 详细

go语言方法实例

时间:2017-06-30 12:33:11      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:.com   参数   domain   poi   struct   logs   new   type   imp   

方便和函数的区别:

方法能给用户定义的类型添加新的行为。方法实际上也是函数,只是在声明时,在关键字func 和方法名之间增加了一个参数。

package main

import (
	"fmt"
)


//define a use struct
type user struct {
	name  string
	email string
}

//notyfy user recieve a method
func (u user) notify() {
	fmt.Printf("Sending User Email to %s<%s>\n",
		u.name,
		u.email)
}


//changeEmail user make a method with pointer
func (u *user) changeEmail(email string) {
	u.email = email
}



//main is the entry of the program
func main() {
	bill := user{"Bill", "bill@email.com"}
	bill.notify()
	
	lisa := &user{"Lisa", "lisa@email.com"}
	lisa.notify()
	
	bill.changeEmail("bill@newdomain.com")
	bill.notify()
	
	lisa.changeEmail("lisa@newdomain.com")
	lisa.notify()

}

  技术分享

go语言方法实例

标签:.com   参数   domain   poi   struct   logs   new   type   imp   

原文地址:http://www.cnblogs.com/aguncn/p/7097952.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!