Skip to content

Commit 14a9364

Browse files
committed
Update README.md
1 parent 0ea1051 commit 14a9364

File tree

1 file changed

+17
-34
lines changed

1 file changed

+17
-34
lines changed

README.md

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -65,53 +65,36 @@ Also, I have an embeddable code editor written in JavaScript which allows editin
6565

6666
## Examples
6767

68-
[demo.go](./docs/examples/demo.go)
68+
[Play Online](https://play.golang.org/p/z7T8ytJ1T1d)
6969

7070
```go
7171
package main
7272

7373
import (
7474
"fmt"
75-
"time"
76-
7775
"github.com/antonmedv/expr"
7876
)
7977

80-
var expressions = []string{
81-
"foo > 0",
82-
"bar.Value in ['a', 'b', 'c']",
83-
"name matches '^hello.+$'",
84-
"now().Sub(startedAt).String()",
85-
"all(tweets, {len(.Message) <= 280}) ? '👍' : '👎'",
86-
}
78+
func main() {
79+
env := map[string]interface{}{
80+
"greet": "Hello, %v!",
81+
"names": []string{"world", "you"},
82+
"sprintf": fmt.Sprintf,
83+
}
8784

88-
var environment = map[string]interface{}{
89-
"foo": 1,
90-
"bar": struct{ Value string }{"c"},
91-
"name": "hello world",
92-
"startedAt": time.Now(),
93-
"now": func() time.Time { return time.Now() },
94-
"tweets": []tweet{{"first tweet"}},
95-
}
85+
code := `sprintf(greet, names[0])`
9686

97-
type tweet struct {
98-
Message string
99-
}
87+
program, err := expr.Compile(code, expr.Env(env))
88+
if err != nil {
89+
panic(err)
90+
}
10091

101-
func main() {
102-
for _, input := range expressions {
103-
program, err := expr.Compile(input, expr.Env(environment))
104-
if err != nil {
105-
panic(err)
106-
}
107-
108-
output, err := expr.Run(program, environment)
109-
if err != nil {
110-
panic(err)
111-
}
112-
113-
fmt.Println(output)
92+
output, err := expr.Run(program, env)
93+
if err != nil {
94+
panic(err)
11495
}
96+
97+
fmt.Println(output)
11598
}
11699
```
117100

0 commit comments

Comments
 (0)