()
| 538 | } |
| 539 | |
| 540 | func ExampleAllowUndefinedVariables_zero_value() { |
| 541 | code := `name == "" ? foo + bar : foo + name` |
| 542 | |
| 543 | // If environment has different zero values, then undefined variables |
| 544 | // will have it as default value. |
| 545 | env := map[string]string{} |
| 546 | |
| 547 | options := []expr.Option{ |
| 548 | expr.Env(env), |
| 549 | expr.AllowUndefinedVariables(), // Allow to use undefined variables. |
| 550 | } |
| 551 | |
| 552 | program, err := expr.Compile(code, options...) |
| 553 | if err != nil { |
| 554 | fmt.Printf("%v", err) |
| 555 | return |
| 556 | } |
| 557 | |
| 558 | env = map[string]string{ |
| 559 | "foo": "Hello, ", |
| 560 | "bar": "world!", |
| 561 | } |
| 562 | |
| 563 | output, err := expr.Run(program, env) |
| 564 | if err != nil { |
| 565 | fmt.Printf("%v", err) |
| 566 | return |
| 567 | } |
| 568 | fmt.Printf("%v", output) |
| 569 | |
| 570 | // Output: Hello, world! |
| 571 | } |
| 572 | |
| 573 | func ExampleAllowUndefinedVariables_zero_value_functions() { |
| 574 | code := `words == "" ? Split("foo,bar", ",") : Split(words, ",")` |
nothing calls this directly
no test coverage detected
searching dependent graphs…