yaegi
yaegi copied to clipboard
How do I call the method of the struct
The following program sample.go
triggers an unexpected result
package script
import (
"fmt"
"github.com/traefik/yaegi/interp"
"github.com/traefik/yaegi/stdlib"
"testing"
)
var (
src2 = `
package plugins_test
import "fmt"
type Test struct {
}
func (t *Test) Run() {
fmt.Println("run")
}
`
)
func TestTest(t *testing.T) {
intp := interp.New(interp.Options{})
intp.Use(stdlib.Symbols)
_, err := intp.Eval(src2)
if err != nil {
panic(err)
}
v, _ := intp.Eval("plugins_test.Test")
fmt.Println(v.MethodByName("Run"))
}
Expected result
Here is the test of calling the structure method, mainly see the following error
Got
=== RUN TestTest
<invalid reflect.Value>
--- PASS: TestTest (0.00s)
PASS
Yaegi Version
v0.11.2
Additional Notes
I want to call the method of this structure, but it seems that I can’t get the method
Has the landlord found a solution?
Looks like https://github.com/traefik/yaegi/blob/master/example/getfunc/getfunc_test.go#L26 might be what you want to try out.