简介
目前go动态库的生产只支持Linux,Windows下会报这样的错误,`-buildmode=plugin not supported on windows/amd64`
CODE
plugin.so
package main
import ( "fmt" ) func DCall(){ fmt.Println("plugin.so was called") } func DCallWithParam(msg string){ fmt.Println("参数内容为:",msg) } func main() { fmt.Println("goroute全部退出") }
pluginload.go
package main
import (
"plugin"
)
func main() {
//加载动态库
p,err := plugin.Open("plugin.so")
if err != nil {
panic(err)
}
//查找函数
f,err := p.Lookup("DCall")
if err != nil {
panic(err)
}
//转换类型后调用函数
f.(func())()
f2,err := p.Lookup("DCallWithParam")
if err != nil {
panic(err)
}
//带参函数的调用
f2.(func(string))("hello world,plugin.so")
}
编译动态库
go build --buildmode=plugin plugin.go
调用动态库
go run pluginload.go