| 1 | <p align="center"> |
| 2 | <img width="400" src="doc/images/yaegi.png" alt="Yaegi" title="Yaegi" /> |
| 3 | </p> |
| 4 | |
| 5 | [](https://github.com/traefik/yaegi/releases) |
| 6 | [](https://github.com/traefik/yaegi/actions/workflows/main.yml) |
| 7 | [](https://pkg.go.dev/mod/github.com/traefik/yaegi) |
| 8 | |
| 9 | Yaegi is Another Elegant Go Interpreter. |
| 10 | It powers executable Go scripts and plugins, in embedded interpreters or interactive shells, on top of the Go runtime. |
| 11 | |
| 12 | ## Features |
| 13 | |
| 14 | * Complete support of [Go specification][specs] |
| 15 | * Written in pure Go, using only the standard library |
| 16 | * Simple interpreter API: `New()`, `Eval()`, `Use()` |
| 17 | * Works everywhere Go works |
| 18 | * All Go & runtime resources accessible from script (with control) |
| 19 | * Security: `unsafe` and `syscall` packages neither used nor exported by default |
| 20 | * Support the latest 2 major releases of Go (Go 1.21 and Go 1.22) |
| 21 | |
| 22 | ## Install |
| 23 | |
| 24 | ### Go package |
| 25 | |
| 26 | ```go |
| 27 | import "github.com/traefik/yaegi/interp" |
| 28 | ``` |
| 29 | |
| 30 | ### Command-line executable |
| 31 | |
| 32 | ```bash |
| 33 | go install github.com/traefik/yaegi/cmd/yaegi@latest |
| 34 | ``` |
| 35 | |
| 36 | Note that you can use [rlwrap](https://github.com/hanslub42/rlwrap) (install with your favorite package manager), |
| 37 | and alias the `yaegi` command in `alias yaegi='rlwrap yaegi'` in your `~/.bashrc`, to have history and command line edition. |
| 38 | |
| 39 | ### CI Integration |
| 40 | |
| 41 | ```bash |
| 42 | curl -sfL https://raw.githubusercontent.com/traefik/yaegi/master/install.sh | bash -s -- -b $GOPATH/bin v0.9.0 |
| 43 | ``` |
| 44 | |
| 45 | ## Usage |
| 46 | |
| 47 | ### As an embedded interpreter |
| 48 | |
| 49 | Create an interpreter with `New()`, run Go code with `Eval()`: |
| 50 | |
| 51 | ```go |
| 52 | package main |
| 53 | |
| 54 | import ( |
| 55 | "github.com/traefik/yaegi/interp" |
| 56 | "github.com/traefik/yaegi/stdlib" |
| 57 | ) |
| 58 | |
| 59 | func main() { |
| 60 | i := interp.New(interp.Options{}) |
| 61 | |
| 62 | i.Use(stdlib.Symbols) |
| 63 | |
| 64 | _, err := i.Eval(`import "fmt"`) |
| 65 | if err != nil { |
| 66 | panic(err) |
| 67 | } |
| 68 | |
| 69 | _, err = i.Eval(`fmt.Println("Hello Yaegi")`) |
| 70 | if err != nil { |
| 71 | panic(err) |
| 72 | } |
| 73 | } |
| 74 | ``` |
| 75 | |
| 76 | [Go Playground](https://play.golang.org/p/2n-EpZbMYI9) |
| 77 | |
| 78 | ### As a dynamic extension framework |
| 79 | |
| 80 | The following program is compiled ahead of time, except `bar()` which is interpreted, with the following steps: |
| 81 | |
| 82 | 1. use of `i.Eval(src)` to evaluate the script in the context of interpreter |
| 83 | 2. use of `v, err := i.Eval("foo.Bar")` to get the symbol from the interpreter context, as a `reflect.Value` |
| 84 | 3. application of `Interface()` method and type assertion to convert `v` into `bar`, as if it was compiled |
| 85 | |
| 86 | ```go |
| 87 | package main |
| 88 | |
| 89 | import "github.com/traefik/yaegi/interp" |
| 90 | |
| 91 | const src = `package foo |
| 92 | func Bar(s string) string { return s + "-Foo" }` |
| 93 | |
| 94 | func main() { |
| 95 | i := interp.New(interp.Options{}) |
| 96 | |
| 97 | _, err := i.Eval(src) |
| 98 | if err != nil { |
| 99 | panic(err) |
| 100 | } |
| 101 | |
| 102 | v, err := i.Eval("foo.Bar") |
| 103 | if err != nil { |
| 104 | panic(err) |
| 105 | } |
| 106 | |
| 107 | bar := v.Interface().(func(string) string) |
| 108 | |
| 109 | r := bar("Kung") |
| 110 | println(r) |
| 111 | } |
| 112 | ``` |
| 113 | |
| 114 | [Go Playground](https://play.golang.org/p/WvwH4JqrU-p) |
| 115 | |
| 116 | ### As a command-line interpreter |
| 117 | |
| 118 | The Yaegi command can run an interactive Read-Eval-Print-Loop: |
| 119 | |
| 120 | ```console |
| 121 | $ yaegi |
| 122 | > 1 + 2 |
| 123 | 3 |
| 124 | > import "fmt" |
| 125 | > fmt.Println("Hello World") |
| 126 | Hello World |
| 127 | > |
| 128 | ``` |
| 129 | |
| 130 | Note that in interactive mode, all stdlib package are pre-imported, |
| 131 | you can use them directly: |
| 132 | |
| 133 | ```console |
| 134 | $ yaegi |
| 135 | > reflect.TypeOf(time.Date) |
| 136 | : func(int, time.Month, int, int, int, int, int, *time.Location) time.Time |
| 137 | > |
| 138 | ``` |
| 139 | |
| 140 | Or interpret Go packages, directories or files, including itself: |
| 141 | |
| 142 | ```console |
| 143 | $ yaegi -syscall -unsafe -unrestricted github.com/traefik/yaegi/cmd/yaegi |
| 144 | > |
| 145 | ``` |
| 146 | |
| 147 | Or for Go scripting in the shebang line: |
| 148 | |
| 149 | ```console |
| 150 | $ cat /tmp/test |
| 151 | #!/usr/bin/env yaegi |
| 152 | package main |
| 153 | |
| 154 | import "fmt" |
| 155 | |
| 156 | func main() { |
| 157 | fmt.Println("test") |
| 158 | } |
| 159 | $ ls -la /tmp/test |
| 160 | -rwxr-xr-x 1 dow184 dow184 93 Jan 6 13:38 /tmp/test |
| 161 | $ /tmp/test |
| 162 | test |
| 163 | ``` |
| 164 | |
| 165 | ## Documentation |
| 166 | |
| 167 | Documentation about Yaegi commands and libraries can be found at usual [godoc.org][docs]. |
| 168 | |
| 169 | Key documentation of the internal design: https://marc.vertes.org/yaegi-internals/ Also see [interp/trace.go](interp/trace.go) for helpful printing commands to see what is happening under the hood during compilation. |
| 170 | |
| 171 | ## Limitations |
| 172 | |
| 173 | Beside the known [bugs] which are supposed to be fixed in the short term, there are some limitations not planned to be addressed soon: |
| 174 | |
| 175 | - Assembly files (`.s`) are not supported. |
| 176 | - Calling C code is not supported (no virtual "C" package). |
| 177 | - Directives about the compiler, the linker, or embedding files are not supported. |
| 178 | - Interfaces to be used from the pre-compiled code can not be added dynamically, as it is required to pre-compile interface wrappers. |
| 179 | - Representation of types by `reflect` and printing values using %T may give different results between compiled mode and interpreted mode. |
| 180 | - Interpreting computation intensive code is likely to remain significantly slower than in compiled mode. |
| 181 | |
| 182 | Go modules are not supported yet. Until that, it is necessary to install the source into `$GOPATH/src/github.com/traefik/yaegi` to pass all the tests. |
| 183 | |
| 184 | ## Contributing |
| 185 | |
| 186 | [Contributing guide](CONTRIBUTING.md). |
| 187 | |
| 188 | ## License |
| 189 | |
| 190 | [Apache 2.0][License]. |
| 191 | |
| 192 | [specs]: https://golang.org/ref/spec |
| 193 | [docs]: https://pkg.go.dev/github.com/traefik/yaegi |
| 194 | [license]: https://github.com/traefik/yaegi/blob/master/LICENSE |
| 195 | [github]: https://github.com/traefik/yaegi |
| 196 | [bugs]: https://github.com/traefik/yaegi/issues?q=is%3Aissue+is%3Aopen+label%3Abug |