I solved it for now using composition and Constructors

func NewA() {
  r := A{}
  r.private = privateForA
} 

func NewB(){
  r := B{}
  r.private = privateForB
}

I saw using constructors in a few builtin packages so I guess is ok.



On Friday, September 1, 2017 at 5:50:52 PM UTC+3, BeaT Adrian 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
>
>
>
> I just want to rewrite a small part of the algorithm.
> As a workaround I used composition, but in a weird way
> type A struct {
>  private
> }
>
> func (a *A) Public() {
>    if a.private == nil {
>      //set private for A
>    }
>    a.private
> }
> type B struct {A}
> func (b *B) Public() {
>  if b.private == nil {
>   //set private for B
>  }
> }
>
>  
>
> Other solutions I tried were not effective (how to detect in inner A 
> function that it's a B now, and should use other algorithm).
>
> Thanks!
>
>
>
>

-- 
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