4 ms·
> But you cannot do the reverse: you cannot make a type from a foreign package conform to your interface by adding new methods to it. I thought you could. You
by justinsaccount 11y ago
> But you cannot do the reverse: you cannot make a type from a foreign package conform to your interface by adding new methods to it.
I thought you could.
You don't add the methods directly to it, but you can easily embed the foreign type into a new type that confirms to the interface you want.
type FooWrapper struct {
Foo
}
func (fw FooWrapper) SomeFunc() {
...
}
- pcwalton 11y agoThat's making a new type, which causes a lot of friction. For example, a []Foo array is not castable to a []FooWrapper array without recreating the entire thing.