1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
package interp_test
import (
"fmt"
"log"
"github.com/traefik/yaegi/interp"
)
// Generic example.
func Example_eval() {
// Create a new interpreter context
i := interp.New(interp.Options{})
// Run some code: define a new function
_, err := i.Eval("func f(i int) int { return 2 * i }")
if err != nil {
log.Fatal(err)
}
// Access the interpreted f function with Eval
v, err := i.Eval("f")
if err != nil {
log.Fatal(err)
}
// Returned v is a reflect.Value, so we can use its interface
f, ok := v.Interface().(func(int) int)
if !ok {
log.Fatal("type assertion failed")
}
// Use interpreted f as it was pre-compiled
fmt.Println(f(2))
// Output:
// 4
}
|