Documentation ¶
Overview ¶
Package gocalc is for evaluating mathematical expressions at runtime.
See https://github.com/justinsacbibit/gocalc-cl-sample for a sample console app that uses gocalc.
func main() { expression, err := gocalc.NewExpr("1 + 2") if err != nil { fmt.Println(err) return } result, err := expression.Evaluate(nil, nil) if err != nil { fmt.Println(err) return } fmt.Println(result) }
Example (CompileError) ¶
_, err := NewExpr("1 +") if err != nil { // A compilation error has occurred }
Output:
Example (EvaluationError) ¶
expression, _ := NewExpr("a") _, err := expression.Evaluate(nil, nil) if err != nil { // An evaluation error has occurred }
Output:
Example (Float) ¶
expression, _ := NewExpr("3.25 * 10") result, _ := expression.Evaluate(nil, nil) fmt.Println(result)
Output: 32.5
Example (Function) ¶
expression, _ := NewExpr("sub2(5)") result, _ := expression.Evaluate(nil, func(fn string, args ...func() interface{}) (result interface{}, handled bool) { switch fn { case "sub2": if len(args) != 1 { panic(EvaluationError("sub2 requires 1 argument")) } arg := args[0]().(int64) return arg - 2, true } return nil, false }) fmt.Println(result)
Output: 3
Example (Identifier) ¶
expression, _ := NewExpr("a * 2") result, _ := expression.Evaluate(func(id string) interface{} { switch id { case "a": return 2 } return nil }, nil) fmt.Println(result)
Output: 4
Example (Simple) ¶
expression, _ := NewExpr("1 + 2") result, _ := expression.Evaluate(nil, nil) fmt.Println(result)
Output: 3
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type EvaluationError ¶
type EvaluationError string
EvaluationError is the type of an expression evaluation error.
func (EvaluationError) Error ¶
func (e EvaluationError) Error() string
Error is EvaluationError's implementation of the error interface.
type Expression ¶
type Expression struct {
// contains filtered or unexported fields
}
An Expression is used to compile and evaluate a string representation of a mathematical expression. Multiple goroutines can use an Expression, as parameters and functions are not stored within the Expression, and are instead resolved during each evaluation.
func NewExpr ¶
func NewExpr(expr string) (*Expression, error)
NewExpr initializes and returns an Expression given the string representation, or an error if compilation failed.
func (*Expression) Evaluate ¶
func (e *Expression) Evaluate(p ParamResolver, f FuncHandler) (result interface{}, err error)
Evaluate evaluates an Expression. If any parameters or function are found, Evaluate will call the appropriate resolver. The evaluation result is returned, or an error if evaluation failed.
type FuncHandler ¶
FuncHandler handles evaluates a function within an Expression, given parameters (which are wrapped in a function for lazy evaluation). FuncHandlers may make calls to panic() with an EvaluationError.
type ParamResolver ¶
type ParamResolver func(string) (value interface{})
ParamResolver resolves the values of any identifiers within an Expression.