Documentation ¶
Overview ¶
Package interp provides a complete Go interpreter.
For the Go language itself, refer to the official Go specification https://golang.org/ref/spec.
Importing packages ¶
Packages can be imported in source or binary form, using the standard Go import statement. In source form, packages are searched first in the vendor directory, the preferred way to store source dependencies. If not found in vendor, sources modules will be searched in GOPATH. Go modules are not supported yet by yaegi.
Binary form packages are compiled and linked with the interpreter executable, and exposed to scripts with the Use method. The goexports command can be used to generate package wrappers.
Custom build tags ¶
Custom build tags allow to control which files in imported source packages are interpreted, in the same way as the "-tags" option of the "go build" command. Setting a custom build tag spans globally for all future imports of the session.
A build tag is a line comment that begins
// yaegi:tags
that lists the build constraints to be satisfied by the further imports of source packages.
For example the following custom build tag
// yaegi:tags noasm
Will ensure that an import of a package will exclude files containing
// +build !noasm
And include files containing
// +build noasm
Example (Eval) ¶
Generic example
package main import ( "fmt" "log" "github.com/containous/yaegi/interp" ) func main() { // 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
Index ¶
- Variables
- type Exports
- type Interpreter
- func (interp *Interpreter) Eval(src string) (reflect.Value, error)
- func (interp *Interpreter) EvalWithContext(ctx context.Context, src string) (reflect.Value, error)
- func (interp *Interpreter) REPL(in io.Reader, out io.Writer)
- func (interp *Interpreter) Repl(in, out *os.File)
- func (interp *Interpreter) Use(values Exports)
- type Options
- Bugs
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Symbols = Exports{ // contains filtered or unexported fields }
Symbols exposes interpreter values
Functions ¶
This section is empty.
Types ¶
type Interpreter ¶
type Interpreter struct { Name string // program name // contains filtered or unexported fields }
Interpreter contains global resources and state
func (*Interpreter) Eval ¶
func (interp *Interpreter) Eval(src string) (reflect.Value, error)
Eval evaluates Go code represented as a string. It returns a map on current interpreted package exported symbols
func (*Interpreter) EvalWithContext ¶ added in v0.7.0
EvalWithContext evaluates Go code represented as a string. It returns a map on current interpreted package exported symbols.
func (*Interpreter) REPL ¶ added in v0.7.0
func (interp *Interpreter) REPL(in io.Reader, out io.Writer)
REPL performs a Read-Eval-Print-Loop on input reader. Results are printed on output writer.
func (*Interpreter) Repl ¶
func (interp *Interpreter) Repl(in, out *os.File)
Repl performs a Read-Eval-Print-Loop on input file descriptor. Results are printed on output. Deprecated: use REPL instead
func (*Interpreter) Use ¶
func (interp *Interpreter) Use(values Exports)
Use loads binary runtime symbols in the interpreter context so they can be used in interpreted code
Notes ¶
Bugs ¶
Type checking is not implemented yet.