go pointer value method and receiver methods recipient

First, the type of object types and object pointer call

    Go language has value types and pointer types directly call its value recipient methods and pointers recipient methods distinguished:

Look at an example:

package main

import "fmt"

type IFather interface {
	getName() string
	setName(string)
}

type Person struct {
	name string
}

func(p Person) getName() string {
	return p.name
}

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

func main() {
	per := Person{}
	fmt.Printf("未初始化默认值:per :%s\n",per.getName())
	per.setName("test")
	fmt.Printf("设置值后:per :%s\n",per.getName())

	per2 := &Person{name:"lisi"}
	fmt.Printf("初始化值后:per2 :%s\n",per2.getName())
	per2.setName("wanger")
	fmt.Printf("设置值后:per2 :%s\n",per2.getName())
}

Output:

未初始化默认值:per1 :
设置值后:per1 :test
初始化值后:per2 :lisi
设置值后:per2 :wanger

   And pointer type variable value type variables whose values ​​can be called a method and receiver pointers recipient methods.

   Method declared value of the recipient, will be used when calling a copy of this value to perform, while the pointer when you call recipient share the recipient calls the method pointed to value, that value can be changed points.

Second, the interface and the interface pointer to call

    Recipients and the pointer value assigned to the receiver interface differences:

   Or the above example a little change it: put the object is assigned to the interface

package main

import "fmt"

type IFather interface {
	getName() string
	setName(string)
}

type Person struct {
	name string
}

func(p Person) getName() string {
	return p.name
}

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

func main() {
	var IPer IFather = &Person{}
	var IPer2 IFather = Person{name:"lisi"}
	fmt.Printf("未初始化默认值:s1:%s\n",IPer.getName())
	IPer.setName("test")
	fmt.Printf("设置值后:s1:%s\n",IPer.getName())
}

 
Compile error: in this line appears

var IPer2 IFather = Person{name:"lisi"}

  Person does not implement the interface IFather the setName function, it can not be assigned.

  On the surface * Person did not realize getName function, but * Person pointed to realize the value of getName function, so * Person also automatically have getName function, so the implementation of the interface IFather. Person as the value of the recipient, Go not know what the original value, because the value of the recipient are copies, it can not affect the Person. pointer will go to give implicit conversion value, but not vice versa.

Therefore, the type of the variable T is T way only the recipient; and has a variable type * T * T T and the recipient is a method , in other words, when a recipient is achieved value type of method, it is possible to automatically generate a the recipient is a method corresponding to the type of pointer, since both will not affect the receiver. However, when a recipient is achieved pointer type method, if the recipient is at this time automatically generates a value type of the method, the desired changes to the original recipient (via a pointer), now can not be achieved because the value type will produce a copy, not really affect the caller.

  (1) Type pointer variable * T may receive and T * T method

  (2) received only type T T method

Published 343 original articles · won praise 57 · Views 200,000 +

Guess you like

Origin blog.csdn.net/jadeshu/article/details/103581572