A high-performance Go expression parser that supports numeric calculations, string concatenation, function calls, conditional statements, function definitions, and registration.
- ✨ Numeric Operations: Supports +, -, *, /, and () operators
- 🔤 String Concatenation: Using + operator
- 📝 Variable Substitution: Variables with $ prefix
- 🎯 Function Calls: Functions with @ prefix
- ⚡ Conditional Statements: Supports >, <, >=, <=, ==, !=, &&, ||, ! operators
- 🚀 High Performance: Excellent concurrent execution performance
Numeric expressions must be wrapped in parentheses:
(1+2)*3 # Result: 9
Use + operator to concatenate strings, strings must be wrapped in double quotes:
"hello"+"world" # Result: helloworld
Variables must start with $, underscores are allowed but other special characters are not:
$price+100 # price is a variable, value is passed during execution
Function calls must start with @:
@calculate($price, 100) # Calling calculate function
Supports complex conditional logic:
($price >= 100 && $price < 200) || @isVIP($userId)
// Function definition
type Function func(args ...any) any
// Register function
RegisterFunc(name string, f Function)
// Simple calculation
expr, err := ParseExpression("($price+100)*0.8")
result := expr.Execute(map[string]any{"price": 200})
// Conditional evaluation
expr, err := ParseExpression(`$price > 100 && @isVIP($userId)`)
result := expr.Execute(map[string]any{
"price": 150,
"userId": "user123",
})
go test -bench=. -benchmem -tags -v
goos: darwin
goarch: arm64
pkg: github.com/go-parser/parser
cpu: Apple M1 Pro
BenchmarkHardCode-8 6342241 174.6 ns/op 112 B/op 3 allocs/op
BenchmarkHardCodeParallel-8 17822409 68.70 ns/op 112 B/op 3 allocs/op
BenchmarkGoParserExecute-8 6851533 174.5 ns/op 112 B/op 4 allocs/op
BenchmarkGoParserExecuteParallel-8 20086538 63.08 ns/op 112 B/op 4 allocs/op
BenchmarkExprLangExecute-8 2112973 558.4 ns/op 368 B/op 14 allocs/op
BenchmarkExprLangExecuteParallel-8 5690395 234.4 ns/op 368 B/op 14 allocs/op
PASS
ok github.com/go-parser/parser 8.986s