Consider the following code:
package main
import "fmt"
// 5
type I interface {
Foo() string
}
type a struct {
i int
}
func (s a) Foo() string {
return "We are here!"
}
func (s a) Bar() string {
return "Major Tom!"
}
// 20
func main() {
var x I = &a{i: 42}
fmt.Println(x.Foo())
fmt.Println(x.(*a).Bar())
}
The last statement of main gives me back the underlying struct but i need to export this struct to get back at it.
If I am using a package in a library where the only symbol exported is the interface (big I, little a in our example above between lines 5-20), then I am unable to cast the interface to the original type when I am in another package or file.
Because the original struct is stored in the interface, is there a simple way to get back its reference and use methods that haven't been declared in the interface and only attached to the struct.
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