我用 Go 編寫了一個插件生成器。回購是開放的。該項目在一些臨時文件中創建了一些 Go 代碼,定義了一個從命令行參數中獲取的函數,將該代碼編譯成一個插件,加載該插件,獲取其中的函數,并使用參數調用它,然后打印結果。我們希望能夠處理多個函數和多個插件。例如,SUMbody 的函數, body 的 return x+y;函數;等等。PRODreturn x*y我不希望生成的代碼始終使用常量名稱 FUNCTION。生成的 .go 文件不能包含名稱在運行時給出的函數,即funame 下面代碼中的 my 嗎?Go 語言是否有某些功能禁止這樣做?//TODO: Investigate how to relax the name FUNCTION into a variabletype Xinterface interface { FUNCTION(x int, y int) int}用法示例:$ go run forbasile.go SUM 'return x+y' 3 5func(s sum) FUNCTION (x int, y int) int { start of sum: x=3, y=5131 bytes written successfully/tmp/go-build104174513/b001/execompiling plugin[]loading modulelooking up symbolchecking module8Generated code: /tmp/SUM.goGenerated object file: /tmp/SUM.so$ go run forbasile.go SUMSQUARE 'return x*x + y*y' 3 4func(s sumsquare) FUNCTION (x int, y int) int { start of sumsquare: x=3, y=4161 bytes written successfully/tmp/go-build555823501/b001/execompiling plugin[]loading modulelooking up symbolchecking module25Generated code: /tmp/SUMSQUARE.goGenerated object file: /tmp/SUMSQUARE.so
1 回答

侃侃無極
TA貢獻2051條經驗 獲得超10個贊
godoc
Symbol 是指向變量或函數的指針。
例如,一個插件定義為
package main
import "fmt"
func F() { fmt.Printf("Hello, number %d\n", V) }
可以加載Open函數,然后可以訪問導出的包符號V和F
p, err := plugin.Open("plugin_name.so")
if err != nil {
? ? panic(err)
}
f, err := p.Lookup("F")
if err != nil {
? ? panic(err)
}
f.(func())() // prints "Hello, number 7"
“F”只是一個字符串,因此您無論如何都可以在運行時更改它的值。
- 1 回答
- 0 關注
- 139 瀏覽
添加回答
舉報
0/150
提交
取消