You may have to resort to a library that interprets mathematical statements or must write its own parser. Python, a dynamic language, can parse and execute python code at runtime. Standard Go cannot do this.
If you want to write a parser yourself, the go package will be useful. Example ( In game ):
import ( "go/ast" "go/parser" "go/token" ) func main() { fs := token.NewFileSet() tr, _ := parser.ParseExpr("(3-1) * 5") ast.Print(fs, tr) }
The resulting AST (abstract syntax tree) can then be traversed and interpreted of your choice (for example, processing "+" tokens as an addition for already saved values).
source share