• Stars
    star
    6,400
  • Rank 5,900 (Top 0.2 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created over 6 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Yaegi is Another Elegant Go Interpreter

Yaegi

release Build Status GoDoc

Yaegi is Another Elegant Go Interpreter. It powers executable Go scripts and plugins, in embedded interpreters or interactive shells, on top of the Go runtime.

Features

  • Complete support of Go specification
  • Written in pure Go, using only the standard library
  • Simple interpreter API: New(), Eval(), Use()
  • Works everywhere Go works
  • All Go & runtime resources accessible from script (with control)
  • Security: unsafe and syscall packages neither used nor exported by default
  • Support the latest 2 major releases of Go (Go 1.21 and Go 1.22)

Install

Go package

import "github.com/traefik/yaegi/interp"

Command-line executable

go install github.com/traefik/yaegi/cmd/yaegi@latest

Note that you can use rlwrap (install with your favorite package manager), and alias the yaegi command in alias yaegi='rlwrap yaegi' in your ~/.bashrc, to have history and command line edition.

CI Integration

curl -sfL https://raw.githubusercontent.com/traefik/yaegi/master/install.sh | bash -s -- -b $GOPATH/bin v0.9.0

Usage

As an embedded interpreter

Create an interpreter with New(), run Go code with Eval():

package main

import (
	"github.com/traefik/yaegi/interp"
	"github.com/traefik/yaegi/stdlib"
)

func main() {
	i := interp.New(interp.Options{})

	i.Use(stdlib.Symbols)

	_, err := i.Eval(`import "fmt"`)
	if err != nil {
		panic(err)
	}

	_, err = i.Eval(`fmt.Println("Hello Yaegi")`)
	if err != nil {
		panic(err)
	}
}

Go Playground

As a dynamic extension framework

The following program is compiled ahead of time, except bar() which is interpreted, with the following steps:

  1. use of i.Eval(src) to evaluate the script in the context of interpreter
  2. use of v, err := i.Eval("foo.Bar") to get the symbol from the interpreter context, as a reflect.Value
  3. application of Interface() method and type assertion to convert v into bar, as if it was compiled
package main

import "github.com/traefik/yaegi/interp"

const src = `package foo
func Bar(s string) string { return s + "-Foo" }`

func main() {
	i := interp.New(interp.Options{})

	_, err := i.Eval(src)
	if err != nil {
		panic(err)
	}

	v, err := i.Eval("foo.Bar")
	if err != nil {
		panic(err)
	}

	bar := v.Interface().(func(string) string)

	r := bar("Kung")
	println(r)
}

Go Playground

As a command-line interpreter

The Yaegi command can run an interactive Read-Eval-Print-Loop:

$ yaegi
> 1 + 2
3
> import "fmt"
> fmt.Println("Hello World")
Hello World
>

Note that in interactive mode, all stdlib package are pre-imported, you can use them directly:

$ yaegi
> reflect.TypeOf(time.Date)
: func(int, time.Month, int, int, int, int, int, *time.Location) time.Time
>

Or interpret Go packages, directories or files, including itself:

$ yaegi -syscall -unsafe -unrestricted github.com/traefik/yaegi/cmd/yaegi
>

Or for Go scripting in the shebang line:

$ cat /tmp/test
#!/usr/bin/env yaegi
package main

import "fmt"

func main() {
	fmt.Println("test")
}
$ ls -la /tmp/test
-rwxr-xr-x 1 dow184 dow184 93 Jan  6 13:38 /tmp/test
$ /tmp/test
test

Documentation

Documentation about Yaegi commands and libraries can be found at usual godoc.org.

Limitations

Beside the known bugs which are supposed to be fixed in the short term, there are some limitations not planned to be addressed soon:

  • Assembly files (.s) are not supported.
  • Calling C code is not supported (no virtual "C" package).
  • Directives about the compiler, the linker, or embedding files are not supported.
  • Interfaces to be used from the pre-compiled code can not be added dynamically, as it is required to pre-compile interface wrappers.
  • Representation of types by reflect and printing values using %T may give different results between compiled mode and interpreted mode.
  • Interpreting computation intensive code is likely to remain significantly slower than in compiled mode.

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.

Contributing

Contributing guide.

License

Apache 2.0.

More Repositories

1

traefik

The Cloud Native Application Proxy
Go
47,533
star
2

mesh

Traefik Mesh - Simpler Service Mesh
Go
1,979
star
3

traefik-helm-chart

Traefik Proxy Helm Chart
Smarty
939
star
4

whoami

Tiny Go server that prints os information and HTTP request to output
Go
906
star
5

mocktail

Naive code generator that creates mock implementation using testify.mock
Go
199
star
6

traefik-migration-tool

A migration tool from Traefik v1 to Traefik v2
Go
146
star
7

traefik-library-image

Used to build Official Docker image of Traefik Proxy
Dockerfile
131
star
8

plugindemo

This repository includes an example plugin, for you to use as a reference for developing your own plugins
Go
121
star
9

blog-posts

Jinja
61
star
10

plugin-rewritebody

Rewrite body is a middleware plugin for Traefik which rewrites the HTTP response body by replacing a search regex by a replacement string
Go
50
star
11

paerser

Loads configuration from many sources
Go
47
star
12

plugin-simplecache

Simple cache plugin middleware caches responses on disk
Go
40
star
13

structor

[Messor Structor 🐜] Manage multiple versions of a Mkdocs documentation
Go
37
star
14

plugin-log4shell

Log4Shell is a middleware plugin for Traefik which blocks JNDI attacks based on HTTP header values
Go
37
star
15

faency

Faency is the Traefik Labs React component library
TypeScript
36
star
16

hub-agent-traefik

Traefik Hub agent for Traefik
Go
34
star
17

lobicornis

🤖 [Myrmica Lobicornis 🐜] Bot: Update and Merge Pull Request
Go
29
star
18

hub-agent-kubernetes

Traefik Hub agent for Kubernetes
Go
19
star
19

aloba

🤖 [Myrmica Aloba 🐜] Bot: Add labels and milestone on pull requests and issues
Go
18
star
20

whoamitcp

Tiny Go server that prints os information and TCP request to output
Go
17
star
21

plugin-blockpath

Block Path is a middleware plugin for Traefik which sends an HTTP 403 Forbidden response when the requested HTTP path matches one the configured regular expressions
Go
15
star
22

pluginproviderdemo

This repository includes an example provider plugin, for you to use as a reference for developing your own plugins
Go
13
star
23

mesh-helm-chart

Traefik Mesh - Helm Chart
Mustache
13
star
24

whoamiudp

Tiny Go webserver that prints os information and UDP request to output
Go
9
star
25

gallienii

🤖 [Myrmica Gallienii 🐜] Bot: Keep Forks Synchronized
Go
8
star
26

hub-helm-chart

Traefik Hub helm chart
Smarty
5
star
27

yaegi-talk

Talks about Yaegi
Go
4
star
28

contributors-guide

Contributors Guide
4
star
29

bibikoffi

🤖 [Myrmica Bibikoffi 🐜] Bot: Closes stale issues
Go
4
star
30

kutteri

🤖 [Chalepoxenus Kutteri 🐜] Bot: Track a GitHub repository and publish on Slack
Go
3
star
31

seo-doc

This program aims to process a documentation folder from traefik/doc and iterate each HTML file adding the requirements for a better SEO
HTML
3
star
32

plugindemowasm

This repository includes an example wasm plugin, for you to use as a reference for developing your own plugins
Go
3
star
33

traefikee-helm-chart

This chart installs the Traefik Enterprise on a Kubernetes cluster, an optional subchart of Traefik Mesh is also bundled
Smarty
3
star
34

traefik-hub-helm-chart

Traefik Hub is a Kubernetes-native API Management solution for publishing, securing, and managing APIs. Configuration is driven by Kubernetes CRDs, labels, and selectors for effective GitOps.
Makefile
2
star
35

traefiklabs-header-app

This Project aims to solve the issue to have a consistent header in many sites running different frameworks.
TypeScript
1
star
36

hub-apiportal-ui

CSS
1
star
37

homebrew-tap

Ruby
1
star
38

mixtus

🤖 [Lasius Mixtus 🐜] Bot: Publish Documentation to a GitHub Repository from another
Go
1
star
39

discourse-theme

Theme of Discourse Community Forum
SCSS
1
star