Expression Language provides an engine for parsing, evaluating, compiling, and serializing restricted expressions in Go. It is designed around explicit Go types, interfaces, error handling, and reusable function values.
The package supports variables, arrays and maps, property and method access, custom functions and providers, parsed-expression caching, AST inspection, null-safe access, null coalescing, and a broad set of arithmetic, logical, comparison, string, and collection operators. It is safe for concurrent evaluation after configuration and has no external dependencies.
The package requires Go 1.24 or newer.
go get github.com/lemric/expression-language-gopackage main
import (
"fmt"
"log"
expressionlanguage "github.com/lemric/expression-language-go"
)
func main() {
language := expressionlanguage.New()
value, err := language.Evaluate(
expressionlanguage.Expr("price * quantity"),
expressionlanguage.Variables{
"price": 21,
"quantity": 2,
},
)
if err != nil {
log.Fatal(err)
}
fmt.Println(value) // 42
}Evaluate parses and executes an expression immediately. Compile returns a
reusable Program, while Parse returns a ParsedExpression whose syntax tree
can be inspected, dumped, evaluated, compiled, or serialized.
program, err := language.Compile(
expressionlanguage.Expr("subtotal * (1 - discount)"),
expressionlanguage.Names{
"subtotal": "subtotal",
"discount": "discount",
},
)
if err != nil {
log.Fatal(err)
}
total, err := program(expressionlanguage.Variables{
"subtotal": 125.0,
"discount": 0.2,
})Compilation produces a Go function rather than generated Go source. The returned program can be reused with different variable values.
- Documentation index
- Installation
- Getting started
- Evaluating, compiling, parsing, and linting
- Expression syntax
- Go values, objects, properties, methods, and indexes
- Built-in and custom functions
- Caching
- AST, nodes, and serialization
- Concurrency and guarantees
- Design and behavioral contracts
- API reference
go test ./...
go test -race ./...
go vet ./...Run the complete language benchmark with:
go test -run '^$' -bench '^BenchmarkLanguage$' -benchmemThis package is released under the MIT License.