yaegi icon indicating copy to clipboard operation
yaegi copied to clipboard

How do I call the method of the struct

Open lz520520 opened this issue 2 years ago • 1 comments

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

lz520520 avatar Dec 29 '21 08:12 lz520520

Has the landlord found a solution?

Lcys avatar May 11 '22 09:05 Lcys

Looks like https://github.com/traefik/yaegi/blob/master/example/getfunc/getfunc_test.go#L26 might be what you want to try out.

Beanzilla avatar Oct 28 '22 00:10 Beanzilla