有的同学会疑问自己第一次加载的时候成了,随后发现一模一样的代码再运行一次就报错plugin加载失败了,这是一个很基础的错误,其实阅读过文档就会知道为什么会产生这样的错误了
When a plugin is first opened, the init functions of all packages not
already part of the program are called. The main function is not run.
A plugin is only initialized once, and cannot be closed.
意思很明显,只能使用plugin.Open加载一次,并且这是不能被关闭的(ps:plugin的所有操作都保障线程安全)
也就是说你只要成功加载了一次,在该进程中和其它的进程再次调用Open尝试加载就会报错(但依然会成功获取到我们想要的库指针)
下面是plugin.Open的描述
Open opens a Go plugin. If a path has already been opened, then the
existing *Plugin is returned. It is safe for concurrent use by
multiple goroutines.
那么解决的办法就是利用recover()接收error,保证接下来的代码继续正常执行
defer func() {
if err := recover(); err != nil {
fmt.Println("plugin had been opened")
}
}()



