Debian Codegraph @

interp/example_eval_test.go

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