Introduction to goval
The goval library is a versatile tool for evaluating a variety of arithmetic, string, and logical expressions. Designed for integration into diverse applications, it supports both variable access and custom function calling, enabling developers to extend its capabilities as needed. Operating under the MIT license, goval has proven its reliability and stability in production systems.
Key Features
-
Expression Evaluation: The core functionality of goval is to interpret and evaluate expressions. This includes basic arithmetic comparisons, logical operations, and string manipulations.
-
Variable Access: Users can define and use variables within expressions. These variables are read-only, ensuring that expressions do not unintentionally alter their original state.
-
Custom Functions: Developers can enhance goval's capabilities by defining custom functions. These functions can be as simple or complex as needed, such as calculating the length of a string or conducting regex operations.
-
Type Support: goval supports several data types, including
bool
,int
,float64
,string
, arrays, and objects. While it handles numbers intuitively by converting between integers and floating-point numbers when necessary, it chooses not to support structs to maintain simplicity and clarity in operation.
Usage Examples
-
Basic Expression Evaluation:
eval := goval.NewEvaluator() result, err := eval.Evaluate(`42 > 21`, nil, nil) // Returns <true, nil>
-
Working with Variables:
eval := goval.NewEvaluator() variables := map[string]interface{}{ "uploaded": 146, "total": 400, } result, err := eval.Evaluate(`uploaded * 100 / total`, variables, nil) // Returns <36, nil>
-
Custom Functions for Extended Operations:
eval := goval.NewEvaluator() functions := make(map[string]goval.ExpressionFunction) functions["strlen"] = func(args ...interface{}) (interface{}, error) { str := args[0].(string) return len(str), nil } result, err := eval.Evaluate(`strlen("text")`, nil, functions) // Returns <4, nil>
Operator and Syntax Support
goval supports numerous operators, from arithmetic (+
, -
, *
, /
) to logic (&&
, ||
), and offers string and array operations like concatenation. A notable feature is the comprehensive handling of literals, including strings, arrays, and hexadecimal numbers.
The library follows C/C++ precedence rules, ensuring predictability in expression evaluation. Furthermore, it supports ternary operations, providing a straightforward conditional logic mechanism.
Advanced Functionalities
- Array and Object Manipulation: goval allows for slicing and accessing elements within arrays and objects, providing developers with powerful tools for data manipulation.
- Bit Manipulation: It supports logical and bitwise operations, facilitating applications that require such low-level computations.
Conclusion and Comparison
For those considering alternatives, goval presents several advantages over similar libraries like Knetic's govaluate. These include a more intuitive syntax, more comprehensive type and operator support, and performance optimizations. Its parser is both highly efficient and compact, contributing to goval's robustness and speed.
In summary, goval is a powerful and flexible library that caters to developers needing advanced expression evaluation in their applications. Its extensive feature set and ease of integration make it a practical choice for a wide range of use cases.