Weekly GitHub Project "Phase II" govaluate

Source: Internet
Author: User
Tags expression engine
This is a creation in Article, where the information may have evolved or changed.

Evaluation of arbitrary expressions in Golang environment//arbitrary expression evaluation for Golang

name govaluate
Address Github
Author Knetic, etc.
Brief intro Arbitrary expression evaluation for Golang
Brief introduction Evaluation of arbitrary expressions in Golang environment
LICENSE MIT
Stars 245

Govaluate provides an evaluation of any arithmetic/string expression similar to the C language.

Why you should not write an expression directly in your code

There are times when you have no way of knowing what an expression looks like, or you want the expression to be set. This library is useful if you have a bunch of data running on your app, or if you want to allow your users to customize something, or if you're writing a monitoring framework that can get a lot of metrics information and then do some formula calculations.

How to use

You can create a new evaluableexpression, and then call its "Evaluate" method.

123
   expression, err: = Govaluate. Newevaluableexpression ("> 0"); result, err: = expression. Evaluate (nil); //result is now set to "true", the bool value.

So, how do I use parameters?

1234567
expression, err: = Govaluate. Newevaluableexpression ("foo > 0"make (map[string]interface8 ) parameters["foo"-1; result, err: = expression. Evaluate (parameters); //result is now set to "false", the bool value.

This is great, but these can be basically implemented directly using code. So what if the calculations involve some mathematical calculations?

12345678
expression, err: = Govaluate. Newevaluableexpression ("(Requests_made * requests_succeeded/100) >=");p arameters: = Make (map[string]interface{ }, 8) parameters["Requests_made"] = 100;parameters["requests_succeeded"] = 80;result, err: = expression. Evaluate (parameters);//result is today set to "false", the bool value.

The above example returns a Boolean value, in fact, it can return a number.

12345678
expression, err: = Govaluate. Newevaluableexpression ("(mem_used/total_mem) *"make (map[string]  Interface8) parameters["Total_mem"1024x768;p arameters["mem_used"512 ; result, err: = expression. Evaluate (parameters); //result is now set to "50.0", the float64 value.

You can also do some date conversions, as long as you meet Rf3339,iso8061,unix date, or ruby dates format standard. If you're still unsure, take a look at the supported date standards.

1234
   expression, err: = Govaluate. Newevaluableexpression ("' 2014-01-02 ' > ' 2014-01-01 23:59:59 '); result, err: = expression. Evaluate (nil); //Result is now set to True

The expression requires only one syntactic analysis and can be reused multiple times.

1234567
   expression, err: = Govaluate. Newevaluableexpression ("Response_time <="make (map[string]  Interface8) for {parameters["response_time"] = pingsomething (); result, err: = Expression. Evaluate (Parameters)}

With respect to execution order, this library supports the normal C standard execution order. When writing an expression, make sure that you write the operator correctly, or use parentheses to clarify which parts of the expression should run first.

Govaluate uses \ or [] to complete the escape.

Support for custom functions

Support for simple structures (accessors)

Operator support

The Ruleplatform expression engine supports the following operations:
Binary calculator: +-/ & | ^ * % >> <<
Binary comparators: > >= < <= = = = =~!~
Logical operators: | | &&
Brackets: ()
Array-Related:, in (Example 1 in (1, 2, ' foo '), return value TRUE)
Unary calculator:! - ~
Ternary operator:? :
Null-value aggregator:??

For more information, please see https://github.com/Knetic/govaluate

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.