-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
如果我想要调用的函数参数个数和类型不定,应该如何使用? #50
Comments
不行,必须知晓函数签名 |
The JIT compiler in #66 will return the function symbols as a You can simply do: loadable, err = jit.BuildGoFiles(jit.BuildConfig{}, "./path/to/file1.go", "/path/to/file2.go")
// or
loadable, err = jit.BuildGoPackage(jit.BuildConfig{}, "./path/to/package")
// or
loadable, err = jit.BuildGoText(jit.BuildConfig{}, `
package mypackage
import "encoding/json"
func MyFunc(input []byte) (interface{}, error) {
var output interface{}
err := json.Unmarshal(input, &output)
return output, err
}
`)
if err != nil {
panic(err)
}
module, symbols, err = loadable.Load()
if err != nil {
panic(err)
}
MyFunc := symbols["MyFunc"].(func([]byte) (interface{}, error)) |
@eh-steve “.(func([]byte) (interface{}, error)” means you already know function type. |
(Or you can type switch across multiple different function types, or use reflect to check the in/out args and then call it programmatically): switch MyFunc := symbols["MyFunc"].(type) {
case func([]byte) (interface{}, error):
out, err := MyFunc([]byte(`{"a": "b"}`))
case func(in1 []byte, in2 int) (map[string]interface{}, error):
out, err := MyFunc([]byte(`{"a": "b"}`), 5)
} or: v := reflect.ValueOf(symbols["MyFunc"])
t := v.Type()
inputs := t.NumIn()
outputs := t.NumOut()
argv := make([]reflect.Value, inputs)
for i := 0; i < inputs; i++ {
in := t.In(i)
// check the type of `in` and decide where to get the value from/do any type conversion as required
argv[i] = reflect.ValueOf(someInput)
}
results := v.Call(argv) |
如果这个Hello函数参数个数以及类型不定,应该如何使用goloader?
The text was updated successfully, but these errors were encountered: