Skip to content

how to populate a slice of compiled expr programs #742

Answered by antonmedv
x1c1cle asked this question in Q&A
Discussion options

You must be logged in to vote

https://go.dev/play/p/FRHiy-r3RwI

package main

import (
	"fmt"
	"log"

	"github.com/expr-lang/expr"
+	"github.com/expr-lang/expr/vm"
)

func main() {
	fmt.Println("main start")

	env := map[string]any{
		"creditScore": 810,
	}

	expressions := []string{
		"creditScore >= 700",
		"creditScore >= 680",
		"creditScore >= 1000",
	}

	var programs []*vm.Program

	for _, exprStr := range expressions {
		program, err := expr.Compile(exprStr)
		if err != nil {
			log.Fatalf("failed to compile expression: %s", exprStr)
		}
		programs = append(programs, program)
	}

	for _, program := range programs {
		result, err := expr.Run(program, env)
		if err != nil {
			fmt.Println("error evaluating...")
	…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@x1c1cle
Comment options

Answer selected by x1c1cle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants