Yes, in the general case you need type assertion (or a type switch) to get back the wrapped value in an interface value.
But you do not need to get back the stored, concrete value from an interface to be able to call other methods it has (and which are not part of the interface type).
You may type-assert another interface value from an interface value, another interface value whose type contains the methods you do want to call.
See this example:
type Foo interface {
Bar() string
}
type fooImpl struct{}
func (fooImpl) Bar() string { return "Bar from fooImpl" }
func (fooImpl) Baz() string { return "Baz from fooImpl" }
func main() {
var f Foo = &fooImpl{}
if x, ok := f.(interface{ Baz() string }); ok {
fmt.Println(x.Baz())
} else {
fmt.Println("f does not have a Baz() method!")
}
}
Foo
only has a Bar()
method. We have a variable f
of type Foo
, the concrete type it stores is *fooImpl
, which also has another method: fooImpl.Baz()
.
So we can type-assert a value of type interface{ Baz() string }
from it, and simply call Baz()
on the result.
The output of the above is (try it on the Go Playground):
Baz from fooImpl
Type-asserting an interface from another interface value does not require to export the type of the wrapped value.
You may also create a new type for the interface type you type assert, anonymous type is not a requirement:
type MyFoo interface{ Baz() string }
if x, ok := f.(MyFoo); ok {
fmt.Println(x.Baz())
} else {
fmt.Println("f does not have a Baz() method!")
}
Output is the same (try it on the Go Playground).
Hell, you can even "extend" Foo
with the additional methods, and type-assert an "extended" interface:
type MyFoo interface {
Foo
Baz() string
}
if x, ok := f.(MyFoo); ok {
fmt.Println(x.Bar())
fmt.Println(x.Baz())
} else {
fmt.Println("f does not have a Baz() method!")
}
Now in this example x
is both a Foo
and a value that has a Baz()
method. Output (try it on the Go Playground):
Bar from fooImpl
Baz from fooImpl