Skip to content

Commit f88955e

Browse files
added examples folder
1 parent 116b3b3 commit f88955e

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

examples/example-01.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/Pramod-Devireddy/go-exprtk"
7+
)
8+
9+
func main() {
10+
// Create a new exprtk instance
11+
exprtkObj := exprtk.NewExprtk()
12+
13+
// Set the expression
14+
exprtkObj.SetExpression("(x + 2)*(y-2)")
15+
16+
// Add variables of expression
17+
exprtkObj.AddDoubleVariable("x")
18+
exprtkObj.AddDoubleVariable("y")
19+
20+
// Compile the expression after expression and variables declaration
21+
exprtkObj.CompileExpression()
22+
23+
// Set values for the variables
24+
exprtkObj.SetDoubleVariableValue("x", 18)
25+
exprtkObj.SetDoubleVariableValue("y", 32)
26+
27+
// Get the evaluated value
28+
fmt.Println(exprtkObj.GetEvaluatedValue())
29+
30+
// Modify values for the variables
31+
exprtkObj.SetDoubleVariableValue("y", 42)
32+
33+
// Get re-evaluated value
34+
fmt.Println(exprtkObj.GetEvaluatedValue())
35+
}

exprtk.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ package exprtk
44
// #cgo LDFLAGS: -L.
55
// #include "exprtkwrapper.h"
66
import "C"
7-
import "fmt"
7+
import (
8+
"errors"
9+
)
810

911
// GoExprtk ...Exprtk Structure
1012
type GoExprtk struct {
@@ -60,13 +62,12 @@ func (obj GoExprtk) SetVectorVariableValue(varName string, val []float64) {
6062
}
6163

6264
// CompileExpression ... Compiles the Expression
63-
func (obj GoExprtk) CompileExpression() {
65+
func (obj GoExprtk) CompileExpression() error {
6466
value := C.compileExpression(obj.exprtk)
6567
if value == 0 {
66-
fmt.Println("Equation didn't compile")
67-
} else {
68-
fmt.Println("Equation Compiled")
68+
return errors.New("Failed to compile the expression")
6969
}
70+
return nil
7071
}
7172

7273
// GetEvaluatedValue ... Returns the evaluated value

0 commit comments

Comments
 (0)