golang接口值类型接收者和指针类型接收者

值接受者是一个拷贝是一个副本,指针接受者传递的是指针

定义接口和结构体

type Pet interface {
	eat()
}

type Dog struct {
	name string
}

实现接口 接受者是值类型

func (dog Dog) eat() {
	fmt.Printf("dog: %p\n", &dog)
	fmt.Println("dog eat...")
	dog.name = "嘿嘿"
}

测试代码

dog := Dog{name: "点小花"}
fmt.Printf("dog: %p\n", &dog)
dog.eat()
fmt.Printf("dog: %v\n", dog)

在这里插入图片描述
实现接口 接受者是指针类型

func (dog *Dog) eat1() {
	fmt.Printf("dog: %p\n", &dog)
	fmt.Println("dog eat...")
	dog.name = "嘿嘿"
}

测试代码

dog := Dog{name: "花花"}
fmt.Printf("dog: %p\n", &dog)
dog.eat1()
fmt.Printf("dog: %v\n", dog)

在这里插入图片描述