You cannot do this.
1. You cannot add methods to imported types.
2. Embedding is not inheritance. (While sometimes things which
are often done via inheritance can be done with embedding too
embedding simply is not inheritance and leads to a totally different
type.)  
3. Go's slices (and arrays) are not covariant.
What you try to do is simply impossible. You must redesign. 

V.

On Tuesday, 13 October 2020 at 22:26:51 UTC+2 glen....@gmail.com wrote:

>
> Hello,
>
> Context: looked at: 
>
> https://stackoverflow.com/questions/28800672/how-to-add-new-methods-to-an-existing-type-in-go
>   
>
> as well as the discussion on this issue:
> ""cannot define new methods on non-local type ___" is not always an error 
> #31631"
> https://github.com/golang/go/issues/31631
>
> Often, the imported structs are inter-related, and you might be wanting to 
> add a method to a struct that is contained in another external struct 
> directly or as an array. So the solution suggested both in the 
> stackoverflow response, and in the github discussion appear to me (please 
> correct me if I am mistaken) not help with more complex external struct 
> inter-relationships.
>
> Please take a look at my example in the playground: 
> https://play.golang.org/p/c9jJvdVTU5l
>
> I would like to add a method to an external struct, then add an array of 
> these to a struct that contains an array of the original external struct. 
> Using either a new type or embedding a type will not work, creating either 
> a:
> > cannot use make([]bEmbedded, 10) (type []bEmbedded) as type []B in 
> assignment
> or
> > cannot use make([]bType, 10) (type []bType) as type []B in assignment
>
> Am I going about this the wrong way?  :-)  I would appreciate help on this.
>
> [Apologies if this has already been asked and resolved]
>
> Thanks,
> Glen
>
>

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/31862bd4-5d87-4a20-a5c0-46bbae94e042n%40googlegroups.com.

Reply via email to