Files
expr/docs/Getting-Started.md
T

118 lines
2.1 KiB
Markdown

# 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. The
struct fields can be renamed by adding struct tags such as `expr:"timestamp"` in
the example below:
```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 `expr:"Timestamp"`
}
func main() {
code := `map(filter(Tweets, {len(.Text) > 0}), {.Text + Format(.Timestamp)})`
// 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: [Custom functions](Custom-Functions.md)