* fix: finish support of type assertions which was incomplete TypeAssert was optimistically returning ok without verifying that value could be converted to the required interface (in case of type assert of an interface type), or not checking the type in all conditions. There is now a working implements method for itype. Fixes #640. * style: appease lint * fix: remove useless code block * doc: improve comments * avoid test conflict
59 lines
559 B
Go
59 lines
559 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
type T1 struct{}
|
|
|
|
func (t1 T1) f() {
|
|
fmt.Println("T1.f()")
|
|
}
|
|
|
|
func (t1 T1) g() {
|
|
fmt.Println("T1.g()")
|
|
}
|
|
|
|
type T2 struct {
|
|
T1
|
|
}
|
|
|
|
func (t2 T2) f() {
|
|
fmt.Println("T2.f()")
|
|
}
|
|
|
|
type I interface {
|
|
f()
|
|
}
|
|
|
|
func printType(i I) {
|
|
if t1, ok := i.(T1); ok {
|
|
println("T1 ok")
|
|
t1.f()
|
|
t1.g()
|
|
}
|
|
|
|
if t2, ok := i.(T2); ok {
|
|
println("T2 ok")
|
|
t2.f()
|
|
t2.g()
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
println("T1")
|
|
printType(T1{})
|
|
println("T2")
|
|
printType(T2{})
|
|
}
|
|
|
|
// Output:
|
|
// T1
|
|
// T1 ok
|
|
// T1.f()
|
|
// T1.g()
|
|
// T2
|
|
// T2 ok
|
|
// T2.f()
|
|
// T1.g()
|