go语言之进阶篇方法值

1、方法值

示例:

package main

import "fmt"

type Person struct {
	name string //名字
	sex  byte   //性别, 字符类型
	age  int    //年龄
}

func (p Person) SetInfoValue() {
	fmt.Printf("SetInfoValue: %p, %v\n", &p, p)
}

func (p *Person) SetInfoPointer() {
	fmt.Printf("SetInfoPointer: %p, %v\n", p, p)
}

func main() {
	p := Person{"mike", 'm', 18}
	fmt.Printf("main: %p, %v\n", &p, p)

	p.SetInfoPointer() //传统调用方式

	//保存方式入口地址
	pFunc := p.SetInfoPointer //这个就是方法值,调用函数时,无需再传递接收者,隐藏了接收者
	pFunc()                   //等价于 p.SetInfoPointer()

	vFunc := p.SetInfoValue
	vFunc() //等价于 p.SetInfoValue()

}

执行结果:

main: 0xc00005a400, {mike 109 18}
SetInfoPointer: 0xc00005a400, &{mike 109 18}
SetInfoPointer: 0xc00005a400, &{mike 109 18}
SetInfoValue: 0xc00005a4a0, {mike 109 18}

  

 

posted @ 2019-01-10 16:04  努力哥  阅读(493)  评论(0编辑  收藏  举报