(1)编写插件

package main

import "fmt"

var Close []float64

func Sum() float64 {
var sum float64 = 0.0
for i, v := range Close {
sum += v
fmt.Printf("%v ==> v:%p close[i]:%p \n", i, &v, &Close[i])
}

return sum
}

编译:go build -buildmode=plugin
cp plugin_M1.so ../testplugin
(2)调用
package main

import (
"fmt"
"plugin"
)

func main() {
p, err := plugin.Open("plugin_M1.so")
if err != nil {
panic(err)
}

v, err := p.Lookup("Close")
if err != nil {
panic(err)
}

para := v.(*[]float64)
iniarr := make([]float64, 5)
for i := 0; i < len(iniarr); i++ {
iniarr[i] = float64(i)
fmt.Printf("test %v ==> %p\n", i, &iniarr[i])
}
*para = iniarr

v2, err := p.Lookup("Sum")
if err != nil {
panic(err)
}

fun := v2.(func() float64)
fmt.Println(fun())
}
编译输出:

jtwang@localhost:~/code/src/baocang.com/testplugin> go build
jtwang@localhost:~/code/src/baocang.com/testplugin> go run .
test 0 ==> 0xc00012e060
test 1 ==> 0xc00012e068
test 2 ==> 0xc00012e070
test 3 ==> 0xc00012e078
test 4 ==> 0xc00012e080
0 ==> v:0xc000132040 close[i]:0xc00012e060
1 ==> v:0xc000132040 close[i]:0xc00012e068
2 ==> v:0xc000132040 close[i]:0xc00012e070
3 ==> v:0xc000132040 close[i]:0xc00012e078
4 ==> v:0xc000132040 close[i]:0xc00012e080
10