-
The following program package main
import (
"awesomeProject2/account2"
"fmt"
"github.com/containous/yaegi/interp"
)
const src2 = `package foo
import "awesomeProject2/account2"
func Bar(in int) int {
fmt.Println(in)
a := account2.Account2{Bal:in};
return a.Bal;
}`
func Bar(in int) int {
fmt.Println(in)
a := account2.Account2{Bal:in};
return a.Bal;
}
func main() {
fmt.Print(Bar(50 ))
i := interp.New(interp.Options{})
i.Eval(src2)
v, _ := i.Eval("foo.Bar")
bar := v.Interface().(func(int) int)
r := bar(50)
println(r)
} Here is my simple Account2 struct
If I define the account2 struct inside the interpreted code, than it works properly. So it looks like the import is not working from inside the interpreted code. $ go run ./main.go
50
50
50
50 Got: $ go run ./main.go
50
50panic: reflect: call of reflect.Value.Interface on zero Value
goroutine 1 [running]:
reflect.valueInterface(0x0, 0x0, 0x0, 0x1, 0x0, 0x1)
/usr/local/Cellar/go/1.14.2_1/libexec/src/reflect/value.go:998 +0x1a5
reflect.Value.Interface(...)
/usr/local/Cellar/go/1.14.2_1/libexec/src/reflect/value.go:993
main.main()
/Users/omer/go/src/awesomeProject2/main.go:31 +0x15d
Process finished with exit code 2
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
I modified your example so it works correctly. Assume that your package main
import (
"fmt"
"go/build"
"awesomeProject2/account2"
"github.com/traefik/yaegi/interp"
"github.com/traefik/yaegi/stdlib"
)
const src2 = `package foo
import (
"fmt"
"awesomeProject2/account2"
)
func Bar(in int) int {
fmt.Println(in)
a := account2.Account2{Bal:in};
return a.Bal;
}`
func Bar(in int) int {
fmt.Println(in)
a := account2.Account2{Bal: in}
return a.Bal
}
func main() {
fmt.Print(Bar(50))
// Specify a GoPath to default GOPATH so external source packages can be found
i := interp.New(interp.Options{GoPath: build.Default.GOPATH})
i.Use(stdlib.Symbols) // necessary because script depends on stdlib fmt
i.Eval(src2)
v, _ := i.Eval("foo.Bar")
bar := v.Interface().(func(int) int)
r := bar(50)
println(r)
} It gives:
Note that |
Beta Was this translation helpful? Give feedback.
-
Thanks @mvertes . appreciate the response. This particular line: |
Beta Was this translation helpful? Give feedback.
-
I believe the "yaegi extract" command is what you're looking for. |
Beta Was this translation helpful? Give feedback.
I believe the "yaegi extract" command is what you're looking for.