golang plugin模块的使用
生活随笔
收集整理的這篇文章主要介紹了
golang plugin模块的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
共享庫工具
Currently plugins are only supported on Linux and macOS
go plugin的使用方法
使用加載plugin基本流程加載編譯好的插件 plugin.Open("./plugin_doctor.so") (*.so文件路徑相對與可執行文件的路徑,可以是絕對路徑) 尋找插件可到變量 plug.Lookup("Doctor"), TypeAssert: Symbol(interface{}) 轉換成API的接口類型 執行API interface的方法e.g
實驗記錄
[kou@python plugin]$ ls aplugin.go aplugin.so test.go [kou@python plugin]$ pwd /home/kou/go/src/plugin [kou@python plugin]$ cat aplugin.go package main func Add(x, y int) int {return x+y} func Subtract(x, y int) int {return x-y } [kou@python plugin]$ cat test.go package mainimport ("fmt""plugin" )func main(){p, _ := plugin.Open("./aplugin.so")add, _ := p.Lookup("Add")sub, _ := p.Lookup("Subtract")sum := add.(func(int, int)int )(11, 2)fmt.Println(sum)subt := sub.(func(int, int)int )(22,1)fmt.Println(subt)}結果
go build -buildmode=plugin -o aplugin.so aplugin.go
[kou@python plugin]$ go run test.go 13 21參考文獻
http://www.361way.com/go-plugin/5925.html
https://mojotv.cn/go/golang-plugin-tutorial
總結
以上是生活随笔為你收集整理的golang plugin模块的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Time, Clocks, and th
- 下一篇: 缘来还是你剧情介绍