go 类型方法 receiver的理解

来源:互联网 发布:巨人网络怎么样 知乎 编辑:程序博客网 时间:2024/06/04 01:23
package mainimport "fmt"type Person struct{    name string    age int }func (this *Person) Growth(){    this.age ++}func (this *Person) ChangeName(newname string){    this.name = newname}func main(){    p := Person{"wangzy", 30}     p.Growth()    fmt.Printf("%d", p.age)}


因为go不是面向对象的,struct也不是类,没有类方法,但是通过类型方法,提供该struct 的方法,类似类方法了。


可以通过类实例去调用。 感觉完全可以替换的

可以通过该结构体的引用参数传入。 


一开始理解还真费劲。


0 0