Evaluate formula in Go

前端 未结 8 959
梦如初夏
梦如初夏 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:27

    There is no such module in Go. You have to build your own. You could use subpackages of the go package, but they might be overkill for your application.

    0 讨论(0)
  • 2021-01-02 13:28

    For expression or program evaluation, you can build a lexer and parser using lex and yacc, and specify exactly the syntax and semantics of your mini-language. A calculator has always been a standard yacc example, and the go versions of lex and yacc are no different.

    Here's a pointer to the calc example: https://github.com/golang-samples/yacc/tree/master/simple

    0 讨论(0)
提交回复
热议问题