1 回答

TA贡献1773条经验 获得超3个赞
Lookup
返回 a Symbol
,这只是一个空接口。为了使用它,您需要断言您想要的类型。例如,文档Symbol
显示了两个断言为预期类型的符号:
v, err := p.Lookup("V")
if err != nil {
panic(err)
}
f, err := p.Lookup("F")
if err != nil {
panic(err)
}
*v.(*int) = 7
f.(func())() // prints "Hello, number 7"
要在你的程序中做到这一点,创建你想要的类型,在这种情况下是一个接口,因为你正在寻找一个特定的方法集(参见接口的“Tour of Go”部分,特别是隐式实现和类型断言)
V这里我们在主程序中创建接口来定义我们想要的方法,而不是对从返回的符号使用类型断言Lookup:
type V interface {
GetId() string
}
func main() {
p, err := plugin.Open("plugin.so")
if err != nil {
panic(err)
}
s, err := p.Lookup("V")
if err != nil {
panic(err)
}
v := s.(V)
fmt.Println(v.GetId())
}
没有找到匹配的内容?试试慕课网站内搜索吧
- 1 回答
- 0 关注
- 115 浏览
添加回答
举报