Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
123 lines (100 loc) · 2.59 KB
/
Copy pathexample_test.go
File metadata and controls
123 lines (100 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package calc_test
import (
"fmt"
"strconv"
"github.com/etnz/calc"
)
// Using [calc.Int] you can parse basic literals,
// exactly as [strconv.ParseInt] can do, and evaluate more advanced
// expressions.
//
// The example below showcases some more posibilities.
func ExampleInt() {
exp := "2"
v, _ := calc.Int(exp)
fmt.Println("Literal:", exp, "=", v)
// For comparison:
v, _ = strconv.ParseInt(exp, 10, 64)
fmt.Println("Package strconv:", exp, "=", v)
exp = "2+2"
v, _ = calc.Int(exp)
fmt.Println("Algebra:", exp, "=", v)
exp = "1<<100 + 2 - 1<<100"
v, _ = calc.Int(exp)
fmt.Println("Safe overflow:", exp, "=", v)
exp = "0777"
v, _ = calc.Int(exp)
fmt.Println("Octal:", exp, "=", v)
exp = "0xFF"
v, _ = calc.Int(exp)
fmt.Println("Hex:", exp, "=", v)
exp = "0b1010 ^ 0b0101"
v, _ = calc.Int(exp)
fmt.Println("Binary:", exp, "=", v)
exp = "0xFF - 0b11111110"
v, _ = calc.Int(exp)
fmt.Println("Mixed:", exp, "=", v)
// Output:
// Literal: 2 = 2
// Package strconv: 2 = 2
// Algebra: 2+2 = 4
// Safe overflow: 1<<100 + 2 - 1<<100 = 2
// Octal: 0777 = 511
// Hex: 0xFF = 255
// Binary: 0b1010 ^ 0b0101 = 15
// Mixed: 0xFF - 0b11111110 = 1
}
// When writing expressions it can be handy to use predefined constants.
// It is possible to prepare a [calc.Scope] with predefined variables.
func ExampleScope_Assign() {
var (
exp string
v int64
c calc.Scope
)
// prepare the Scope with useful constants.
c.Assign("s", "1") // 1 second
c.Assign("m", "60*s")
c.Assign("h", "60*m")
c.Assign("d", "24*h")
c.Assign("w", "7*d")
// Constants in Go are powerful:
// expression can use floating point and still lead
// to an exact integer.
exp = "2.5*d"
v, _ = c.Int(exp)
fmt.Println("Time:", exp, "=", v)
exp = "2*d + 4*h"
v, _ = c.Int(exp)
fmt.Println("Time:", exp, "=", v)
// Constants in Go are powerful 2
// It doesn't matter how you have defined them.
exp = "2.5*s"
f, _ := c.Float64(exp)
fmt.Println("Float:", exp, "=", f)
// Output:
// Time: 2.5*d = 216000
// Time: 2*d + 4*h = 187200
// Float: 2.5*s = 2.5
}
// When writing expressions using variables, it is possible
// to "pack" them in their own namespace to avoid conflicts.
func ExampleScope_Import() {
var (
exp string
v int64
c, lib calc.Scope
)
// prepare a library of variables.
lib.Assign("S", "1") // 1 second
lib.Assign("M", "60*S")
lib.Assign("H", "60*M")
lib.Assign("D", "24*H")
// Import that lib as 'time' to make it usable.
c.Import("time", &lib)
exp = "2*time.D + 4*time.H"
v, _ = c.Int(exp)
fmt.Println("Time:", exp, "=", v)
// Output:
// Time: 2*time.D + 4*time.H = 187200
}