Maison >développement back-end >Golang >Comment puis-je évaluer des formules dans Go ?
Evaluating Formulas in Go
In Go, evaluating a formula with predefined values requires the use of an external package, such as govaluate. This package provides a convenient method to evaluate formulas represented as strings.
To evaluate a formula, you can follow these steps:
Import the govaluate package:
import "github.com/Knetic/govaluate"
Create a new evaluable expression from the formula string:
expression, err := govaluate.NewEvaluableExpression("(x + 2) / 10")
Define a map of parameter values to be used in the evaluation:
parameters := make(map[string]interface{}, 8) parameters["x"] = 8
Evaluate the expression using the parameter values:
result, err := expression.Evaluate(parameters)
Check for errors and retrieve the result:
if err != nil { // Handle error } fmt.Println(result)
This will print 1, as in the Python example.
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!