Friday, March 24, 2023
HomeGolangWhy does GO compiler include a tip worth receiver approach to the...

Why does GO compiler include a tip worth receiver approach to the approach collection of non-pointer kind when utilizing the guideline Kind Embedding? – Obtaining Assistance


The title may be a little complex since I’m uncertain just how to discuss in one sentence (one inquiry) without an instance and also a much more comprehensive tale.

To plainly discuss what the inquiry in the title indicates, allow me begin with a circumstance that acts as I anticipate.

 plan primary

import (
" fmt".
" mirror".
).

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(" approach #", i, ": ", t1.Method( i). Call, "n").
}

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

When the kind embedding is based upon "kind" (not right 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 approach ( MyType1) Function1() in its approach established and also the kind * MyType1 will certainly have the techniques (* MyType1) Function1() and also (* MyType1) Function2() in its approach established. So, each kind ( MyType1 and also * MyType1) will certainly obtain their equivalent techniques. * MyType1 will certainly obtain the approach (* MyType1) Function1() since it's unconditionally emerges from ( MyType1) Function1() So, as I claimed in the past, whatever is anticipated. To verify this, I additionally utilized the typical "mirror" plan and also obtained the adhering to hard copy:

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

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

 plan primary.

import (.
" fmt".
" mirror".
).

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(" approach #", i, ": ", t1.Method( i). Call, "n").
}

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

}

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

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

So, in some way MyType1 additionally has approach ( MyType1) Function2() despite the fact that it is not proclaimed as worth receiver approach for kind MyType2

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

RELATED ARTICLES

Most Popular

Recent Comments