Wednesday, March 22, 2023
HomeGolangWhy does GO compiler include a tip worth receiver technique to the...

Why does GO compiler include a tip worth receiver technique to the technique collection of non-pointer kind when making use of the reminder Kind Embedding? – Obtaining Aid


The title could be a little complicated since I’m not exactly sure just how to clarify in one sentence (one concern) without an instance and also an extra in-depth tale.

To plainly clarify what the concern in the title indicates, allow me begin with a scenario that acts as I anticipate.

 bundle primary

import (
" fmt".
" show".
).

kind MyType1 struct {
MyType2.
}

kind MyType2 struct {
}

func (MyType2) Function1() {
}

func (* MyType2) Function2() {
}

func primary() {
t1:= reflect.TypeOf( MyType1 {} ).
t2:= reflect.TypeOf(&& MyType1 {} ).

fmt.Println( t1, "has", t1.NumMethod(), "techniques:").
for i:= 0; i < < t1.NumMethod(); i++ {
fmt.Print(" technique #", i, ": ", t1.Method( i). Call, "n").
}

fmt.Println( t2, "has", t2.NumMethod(), "techniques:").
for i:= 0; i < < t2.NumMethod(); i++ {
fmt.Print(" technique #", i, ": ", t2.Method( i). Call, "n").
}
}

When the kind embedding is based upon "kind" (not proper name, yet allow's call it like that) whatever acts as I anticipate. The framework MyType1 has a MyType2 ingrained area (" kind" ingrained area) so the kind MyType1 will certainly have the technique ( MyType1) Function1() in its technique established and also the kind * MyType1 will certainly have the techniques (* MyType1) Function1() and also (* MyType1) Function2() in its technique established. So, each kind ( MyType1 and also * MyType1) will certainly obtain their equivalent techniques. * MyType1 will certainly obtain the technique (* MyType1) Function1() since it's unconditionally develops from ( MyType1) Function1() So, as I claimed in the past, whatever is anticipated. To verify this, I likewise utilized the conventional "show" bundle and also obtained the adhering to hard copy:

 main.MyType1 has 1 techniques:.
technique # 0: Function1.
* main.MyType1 has 2 techniques:.
technique # 0: Function1.
technique # 1: Function2.

An odd actions happens when I change the "kind" ingrained area with a "reminder kind" ingrained area ( MyType1 currently has * MyType2 as opposed to MyType2 ). So the code resembles this:

 bundle primary.

import (.
" fmt".
" show".
).

kind MyType1 struct {
* MyType2.
}

kind MyType2 struct {
}

func (MyType2) Function1() {
}

func (* MyType2) Function2() {
}

func primary() {
t1:= reflect.TypeOf( MyType1 {} ).
t2:= reflect.TypeOf(&& MyType1 {} ).

fmt.Println( t1, "has", t1.NumMethod(), "techniques:").
for i:= 0; i < < t1.NumMethod(); i++ {
fmt.Print(" technique #", i, ": ", t1.Method( i). Call, "n").
}

fmt.Println( t2, "has", t2.NumMethod(), "techniques:").
for i:= 0; i < < t2.NumMethod(); i++ {
fmt.Print(" technique #", i, ": ", t2.Method( i). Call, "n").
}

}

Currently what is in fact the essence of the trouble. The prints is:

 main.MyType1 has 2 techniques:.
technique # 0: Function1.
technique # 1: Function2.
* main.MyType1 has 2 techniques:.
technique # 0: Function1.
technique # 1: Function2.

So, in some way MyType1 likewise has technique ( MyType1) Function2() although it is not stated as worth receiver technique for kind MyType2

Does any individual have any type of rational description regarding why this is taking place?

RELATED ARTICLES

Most Popular

Recent Comments