I’ve noticed I don’t have any way to check if calling a method might cause my program to panic.
package main
type MyInterface interface {
MyMethod()
}
type MyType1 struct {
MyInterface
}
type MyType2 struct {
MyInterface
}
func (mt MyType1) MyMethod() {
println("MyType1.MyMethod called")
}
func main() {
var myInstance MyInterface
var mt1 MyType1
myInstance = mt1
if myInstance.MyMethod != nil {
// this definitely works
myInstance.MyMethod()
}
var mt2 MyType2
myInstance = mt2
//None of the following works to check if MyMethod is nil
//if myInstance.MyMethod != nil {
if _, ok := myInstance.(MyInterface); ok {
// MyMethod is not nil, but this will panic
myInstance.MyMethod()
// How would I know that calling MyMethod cause panic?
}
}
Do I have a way to actually know if I can call MyMethod()
or not?
I tried checking the three mentioned ways to check if I can call, seems like it’s not possible.