Example_cel_Arithmetic showcases negation, basic operations, modulo, and precedence as documented in https://celbyexample.com/arithmetic/
()
| 24 | // Example_cel_Arithmetic showcases negation, basic operations, modulo, and precedence |
| 25 | // as documented in https://celbyexample.com/arithmetic/ |
| 26 | func Example_cel_Arithmetic() { |
| 27 | exprs := []string{ |
| 28 | `-5`, |
| 29 | `10 + 20`, |
| 30 | `30 - 12`, |
| 31 | `6 * 7`, |
| 32 | `20 / 4`, |
| 33 | `7 % 3`, |
| 34 | `2 + 3 * 4`, |
| 35 | `(2 + 3) * 4`, |
| 36 | } |
| 37 | |
| 38 | for _, expr := range exprs { |
| 39 | prg, err := cel.Compile(expr) |
| 40 | if err != nil { |
| 41 | log.Fatalf("cel.Compile() error for %q: %v", expr, err) |
| 42 | } |
| 43 | out, _, err := prg.Eval(cel.NoVars()) |
| 44 | if err != nil { |
| 45 | log.Fatalf("prg.Eval() error for %q: %v", expr, err) |
| 46 | } |
| 47 | fmt.Printf("%s -> %v\n", expr, out) |
| 48 | } |
| 49 | |
| 50 | // Output: |
| 51 | // -5 -> -5 |
| 52 | // 10 + 20 -> 30 |
| 53 | // 30 - 12 -> 18 |
| 54 | // 6 * 7 -> 42 |
| 55 | // 20 / 4 -> 5 |
| 56 | // 7 % 3 -> 1 |
| 57 | // 2 + 3 * 4 -> 14 |
| 58 | // (2 + 3) * 4 -> 20 |
| 59 | } |
| 60 | |
| 61 | // Example_cel_Comparison showcases equality and ordering comparison operators |
| 62 | // as documented in https://celbyexample.com/comparison/ |