11# Getting Started
22
3- ** Expr** provides a package for evaluating arbitrary expressions as well as type checking of such expression.
3+ ** Expr** provides a package for evaluating arbitrary expressions as well as type
4+ checking of such expression.
45
56## Evaluate
67
8+ For simple use cases with one execution of an expression, you can use the
9+ ` expr.Eval ` function. It takes an expression and a map of variables and returns
10+ the result of the expression.
11+
712``` go
813package main
914
@@ -29,7 +34,9 @@ func main() {
2934
3035## Compile
3136
32- Usually we want to compile the code on save (For example, in [ web user interface] ( https://antonmedv.github.io/expr/ ) ).
37+ Usually we want to compile, type check and verify what the expression returns a
38+ boolean (or another type). For example, if a user saves an expression from
39+ [ web UI] ( https://antonmedv.github.io/expr/ ) .
3340
3441``` go
3542package main
@@ -44,13 +51,13 @@ func main() {
4451 env := map [string ]interface {}{
4552 " greet" : " Hello, %v !" ,
4653 " names" : []string {" world" , " you" },
47- " sprintf" : fmt.Sprintf , // You can pass any functions.
54+ " sprintf" : fmt.Sprintf , // Use any functions.
4855 }
4956
5057 code := ` sprintf(greet, names[0])`
5158
52- // Compile code into bytecode. This step can be done once and program may be reused.
53- // Specify environment for type check.
59+ // Compile the code into a bytecode. This step can be done once
60+ // and program may be reused. Specify an environment for type check.
5461 program , err := expr.Compile (code, expr.Env (env))
5562 if err != nil {
5663 panic (err)
@@ -66,8 +73,7 @@ func main() {
6673```
6774
6875You may use existing types. For example, an environment can be a struct. The
69- struct fields can be renamed by adding struct tags such as ` expr:"timestamp" ` in
70- the example below:
76+ struct fields can be renamed by adding struct tags such as ` expr:"name" ` .
7177
7278``` go
7379package main
@@ -83,16 +89,16 @@ type Env struct {
8389 Tweets []Tweet
8490}
8591
86- // Methods defined on such struct will be functions.
92+ // Methods defined on the struct become functions.
8793func (Env ) Format (t time .Time ) string { return t.Format (time.RFC822 ) }
8894
8995type Tweet struct {
9096 Text string
91- Date time.Time ` expr:"Timestamp "`
97+ Date time.Time ` expr:"timestamp "`
9298}
9399
94100func main () {
95- code := ` map(filter(Tweets, {len(.Text) > 0}), {.Text + Format(.Timestamp )})`
101+ code := ` map(filter(Tweets, {len(.Text) > 0}), {.Text + Format(.timestamp )})`
96102
97103 // We can use an empty instance of the struct as an environment.
98104 program , err := expr.Compile (code, expr.Env (Env{}))
@@ -101,7 +107,11 @@ func main() {
101107 }
102108
103109 env := Env{
104- Tweets: []Tweet{{" Oh My God!" , time.Now ()}, {" How you doin?" , time.Now ()}, {" Could I be wearing any more clothes?" , time.Now ()}},
110+ Tweets: []Tweet{
111+ {" Oh My God!" , time.Now ()},
112+ {" How you doin?" , time.Now ()},
113+ {" Could I be wearing any more clothes?" , time.Now ()},
114+ },
105115 }
106116
107117 output , err := expr.Run (program, env)
@@ -113,5 +123,4 @@ func main() {
113123}
114124```
115125
116- - [ Contents] ( README.md )
117- - Next: [ Custom functions] ( Custom-Functions.md )
126+ * Next: [ Operator Overloading] ( Operator-Overloading.md )
0 commit comments