On Sunday, September 3, 2017 at 1:51:54 AM UTC-4, Jesse McNelis wrote:
>
> On Sat, Sep 2, 2017 at 12:50 AM, BeaT Adrian <adi...@gmail.com 
> <javascript:>> wrote: 
> > Hello, I ran into a strange scenario and I wanted to know if there is a 
> > better solution for it 
> > 
> > type A struct{} 
> > 
> > func (a *A) private() {} 
> > func (a *A) Public() { 
> >    a.private() 
> > } 
> > 
> > type B struct {A} 
> > 
> > func (b *B) private() {} 
> > 
> > bi := B{} 
> > b.Public() //calls the A.Private 
> > 
>
> Go doesn't do inheritance only composition with method forwarding. 
> Trying to do it will make you sad. 
>
> The general solution to sharing code between two types is to write a 
> function that takes a common interface that both A and B implement. 
>
> eg. 
>
> type Privater interface { 
>   private() 
> } 
>
> func Public(p Privator){ 
>     p.private() 
> } 
>

Yes, I call it Polymorphism By Interface in Go. Check out more at
https://medium.com/@gianbiondi/interfaces-in-go-59c3dc9c2d98

Also, if you were trying to define a "virtual function" that its behavior 
can be overridden by its "sub-class", check out
https://github.com/suntong/lang/blob/master/lang/Go/src/oo/Polymorphism-AnimalVF3.go
or its previous two versions (AnimalVF1.go & AnimalVF2.go). 


-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to