内容简介:结果如下:
// code_20_struct_method_expression project main.go
package main
import (
"fmt"
)
//方法表达式:也即“方法对象赋值给变量”
//两种使用方式:
//1)隐式调用, struct实例获取方法对象---->方法值
//2)显示调用, struct类型获取方法对象, 须要传递struct实例对象作为参数。---->方法表达式
type Person struct {
name string
sex byte
age int
}
func (p *Person) PrintInfoPointer() {
fmt.Printf("%p, %v\n", p, p)
}
func (p Person) PrintInfoValue() {
fmt.Printf("%p, %v\n", &p, p)
}
func main() {
//直接调用
p := Person{"ck_god", 'm', 18}
p.PrintInfoPointer()
fmt.Println("---------------\n")
//方法表达式
pFunc1 := (*Person).PrintInfoPointer
pFunc1(&p)
pFunc2 := Person.PrintInfoValue
pFunc2(p)
fmt.Println("---------------\n")
//方法值
pFunc3 := p.PrintInfoPointer
pFunc3()
pFunc4 := p.PrintInfoValue
pFunc4()
fmt.Println("---------------\n")
//备注:pFunc2和pFunc4的内存地址是不一样的;pFunc1和pFunc3的内存地址是一致的
}
结果如下:
0xc000050400, &{ck_god 109 18}
---------------
0xc000050400, &{ck_god 109 18}
0xc000050480, {ck_god 109 18}
---------------
0xc000050400, &{ck_god 109 18}
0xc0000504e0, {ck_god 109 18}
---------------
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Lean Startup
Eric Ries / Crown Business / 2011-9-13 / USD 26.00
更多中文介绍:http://huing.com Most startups fail. But many of those failures are preventable. The Lean Startup is a new approach being adopted across the globe, chan ging the way companies are built and ......一起来看看 《The Lean Startup》 这本书的介绍吧!
CSS 压缩/解压工具
在线压缩/解压 CSS 代码
UNIX 时间戳转换
UNIX 时间戳转换