Skip to main content
Version: v1.9

Getting Started

Expr provides a package for evaluating arbitrary expressions as well as type checking of such expression.

Evaluate

package main

import (
"fmt"
"github.com/antonmedv/expr"
)

func main() {
env := map[string]interface{}{
"foo": 1,
"bar": 2,
}

out, err := expr.Eval("foo + bar", env)

if err != nil {
panic(err)
}
fmt.Print(out)
}

Compile

Usually we want to compile the code on save (For example, in web user interface).

package main

import (
"fmt"

"github.com/antonmedv/expr"
)

func main() {
env := map[string]interface{}{
"greet": "Hello, %v!",
"names": []string{"world", "you"},
"sprintf": fmt.Sprintf, // You can pass any functions.
}

code := `sprintf(greet, names[0])`

// Compile code into bytecode. This step can be done once and program may be reused.
// Specify environment for type check.
program, err := expr.Compile(code, expr.Env(env))
if err != nil {
panic(err)
}

output, err := expr.Run(program, env)
if err != nil {
panic(err)
}

fmt.Print(output)
}

You may use existing types. For example, an environment can be a struct.

package main

import (
"fmt"
"time"

"github.com/antonmedv/expr"
)

type Env struct {
Tweets []Tweet
}

// Methods defined on such struct will be functions.
func (Env) Format(t time.Time) string { return t.Format(time.RFC822) }

type Tweet struct {
Text string
Date time.Time
}

func main() {
code := `map(filter(Tweets, {len(.Text) > 0}), {.Text + Format(.Date)})`

// We can use an empty instance of the struct as an environment.
program, err := expr.Compile(code, expr.Env(Env{}))
if err != nil {
panic(err)
}

env := Env{
Tweets: []Tweet{{"Oh My God!", time.Now()}, {"How you doin?", time.Now()}, {"Could I be wearing any more clothes?", time.Now()}},
}

output, err := expr.Run(program, env)
if err != nil {
panic(err)
}

fmt.Print(output)
}