July 2019
Intermediate to advanced
458 pages
12h 12m
English
Assertion can also be done from one interface to another. Imagine having two different interfaces:
type Fooer interface { Foo()}type Barer interface { Bar()}
Let's define a type that implements one of them and another that implements both:
type A intfunc (A) Foo() {}type B intfunc (B) Bar() {}func (B) Foo() {}
If we define a new variable for the first interface, the assertion to the second is going to be successful only if the underlying value has a type that implements both; otherwise, it's going to fail:
func main() { var a Fooer a = A(0) v, ok := a.(Barer) fmt.Println(v, ok) a = B(0) v, ok = a.(Barer) fmt.Println(v, ok)}
A full example is available here: https://play.golang.org/p/bX2rnw5pRXJ.
A use case scenario ...
Read now
Unlock full access