Evaluate formula in Go

前端 未结 8 991
梦如初夏
梦如初夏 2021-01-02 12:46

Using Go (golang) I\'d like to take a string with a formula and evaluate it with pre-defined values. Here\'s a way to do it with python\'s parser module:

<
8条回答
  •  南方客
    南方客 (楼主)
    2021-01-02 13:21

    This package will probably work for your needs: https://github.com/Knetic/govaluate

    expression, err := govaluate.NewEvaluableExpression("(x + 2) / 10");
    
    parameters := make(map[string]interface{}, 8)
    parameters["x"] = 8;
    
    result, err := expression.Evaluate(parameters);
    

提交回复
热议问题