日常生活的交流与学习

首页 新随笔 联系 管理

func (this Person) 和 func (this *Person)的区别

func (this Person)

package main

import (
	"fmt"
)

type Person struct {
	name string
	age  int
}

func (this Person) show() {
	fmt.Println("name=", this.name, "age=", this.age)
}

func (this Person) setName(name string) {
	this.name = name
}

func main() {
	alice := Person{"Alice", 19}
	alice.show()
	fmt.Println("------------")
	alice.setName("alice111")
	alice.show()
}

VVV

name= Alice age= 19
------------
name= alice111 age= 19

func (this *Person)

package main

import (
	"fmt"
)

type Person struct {
	name string
	age  int
}

func (this *Person) show() {
	fmt.Println("name=", this.name, "age=", this.age)
}

func (this *Person) setName(name string) {
	this.name = name
}

func main() {
	alice := Person{"Alice", 19}
	alice.show()
	fmt.Println("------------")
	alice.setName("alice111")
	alice.show()
}

VVV

name= Alice age= 19
------------
name= alice111 age= 19
posted on 2022-10-15 17:20  lazycookie  阅读(49)  评论(0编辑  收藏  举报