MCPcopy
hub / github.com/expr-lang/expr / ExampleAllowUndefinedVariables_zero_value

Function ExampleAllowUndefinedVariables_zero_value

expr_test.go:540–571  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

538}
539
540func 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
573func ExampleAllowUndefinedVariables_zero_value_functions() {
574 code := `words == "" ? Split("foo,bar", ",") : Split(words, ",")`

Callers

nothing calls this directly

Calls 5

EnvStruct · 0.92
AllowUndefinedVariablesFunction · 0.92
CompileFunction · 0.92
RunFunction · 0.92
PrintfMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…