So、dll、dylib的区别
- 主要是不同平台的动态连接库,本质上都是
二进制文件
Golang 编译so文件和使用so文件
Go编译输出so动态链接库
xplugin.so
package main
func Add(x, y int) int {
return x + y
}
func Subtract(x, y int) int {
return x - y
}
go build -buildmode=plugin -o xplugin.so xplugin.go
Go使用so动态链接库
package pkg
import (
"fmt"
"plugin"
"testing"
)
func TestXplugin(t *testing.T) {
ptr, err := plugin.Open("xplugin.so")
if err != nil {
fmt.Println(err)
}
Add, _ := ptr.Lookup("Add")
sum := Add.(func(int, int) int)(5, 4)
fmt.Println("Add结果:", sum)
Sub, _ := ptr.Lookup("Subtract")
sub := Sub.(func(int, int) int)(9, 8)
fmt.Println("Sub结果:", sub)
}
go test -v ./pkg -run TestXplugin