This is a creation in Article, where the information may have evolved or changed.
Note: This article is just my personal note, if there is a fallacy, error, please be sure to point out!
Package Main
Import "FMT"
IA test for Methodset.
Type IA Interface{
Value ()
Pointer ()
}
A is a test type.
type A int
//
Value receiver.
Func (A A) Value () {
Fmt. Printf ("value:%p,%d\n", &a, a)
}
/
/pointer ReceiveR.
Func (a *a) Pointer () {
Fmt. Printf ("pointer:%p,%d\n", A, *a)
}
Func Main () {
//
type A Method set: (A A) Value ()
var a = 1
Fmt. Printf ("Origin:%p,%d\n", &a, a)
Fmt. PRINTLN ("Value receiver------------------------------")
A.value ()
A.pointer ()//such as: (&a).
Value (),
a value converted to a PointerBy go compiler, so value a can call type *a ' s pointer receiver method '
//
type *a method set: func (A A) Value (), func (a *a) Pointer ()
Fmt. PRINTLN ("Pointer receiver---------------------------")
P: = &a
P.value ()
P.pointer ()
Fmt. PRINTLN ("Interface call method by type *a-----------------------")
var ia ia
IA = &a
Ia. Value ()
Ia. Pointer ()
Fmt. PRINTLN ("Interface call method by type A-----------------------")
For the interface, for the type assigned to him, will hold its copy, which will be copied, and its holding value is not the address, there will be more stringent type checking and restrictions, do not allow such as: A to *a conversion,
Therefore, when Func (a *a) Pointer () is called with Type A, it is not allowed because the method set of type A does not include Func (a *a) Pointer (), or it can be said that the method sets of type A are a subset of type *a.
Summary: When you call pointer receiver method and value receiver method as a non-interface type, the Go compiler helps with automatic conversions, such as: &a or *a; Therefore, the bounds of the value receiver and the pointer receiver method set are diluted.
But the interface is not allowed to do these automatic conversions, one is a strict delineation of methods set demarcation, which is Golang interface design philosophy, is not a technical problem,
A interface must check type A or type *a, do not allow to convert it between of them, so type a can not call type *a ' s Poi Nter Receiver Method '
ia = a//compliation error,coz type not match:a does no implement IA (Pointer method has Pointer receiver)
If you change to: IA = &a, because: The method set of type *a includes this method, so it matches the interface.
Ia. Value ()
Ia. Pointer ()
}
Program output:
origin:0xc04200e290, 1//NOTE: Unlike this address, there is a replication, which is the difference between the value receiver and the pointer receiver, the focus.
Value receiver------------------------------
VALUE:0XC04200E2A8, 1
pointer:0xc04200e290, 1
Pointer Receiver---------------------------
VALUE:0XC04200E2F0, 1
pointer:0xc04200e290, 1
Interface call method by type *a-----------------------
value:0xc04200e308, 1
pointer:0xc04200e290, 1
Interface call method by type A-----------------------
Note: This article is only my personal notes, if there are errors and omissions, please correct me, study together, my e-mail: htyu_0203_39@sina.com