【发布时间】:2019-11-26 05:47:37
【问题描述】:
我想通过反射来确定一个 Go 接口是否包含某些方法签名。我之前通过结构上的reflection 动态获得了名称和签名。这是一个简化的示例:
package main
import "reflect"
func main() {
type Mover interface {
TurnLeft() bool
// TurnRight is missing.
}
// How would I check whether TurnRight() bool is specified in Mover?
reflect.TypeOf(Mover).MethodByName("TurnRight") // would suffice, but
// fails because you can't instantiate an interface
}
【问题讨论】:
标签: reflection go