1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
# Getting Started
**Expr** provides a package for evaluating arbitrary expressions as well as type checking of such expression.
## Evaluate
```go
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](https://antonmedv.github.io/expr/)).
```go
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.
```go
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)
}
```
* [Contents](README.md)
* Next: [Operator Override](Operator-Override.md)
|