forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profiling.go
72 lines (57 loc) · 1.3 KB
/
profiling.go
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
package main
import (
"fmt"
"log"
"strconv"
"strings"
)
var vars = map[string]string{
"variable_name": "bill",
}
// main used the function that will be testing and showing how to review
// cpu and memory profiling information.
func main() {
variable := "#string:variable_name"
v := getValue(variable, vars)
v, ok := v.(string)
if !ok {
log.Fatalln("Invalid type was returned")
}
if v != `"bill"` {
log.Fatalf("Invalid value was returned : %v", v)
}
log.Printf("Value was returned : %v", v)
}
// getValue takes a string with a specialized format, processes it and return
// an native value.
func getValue(variable string, vars map[string]string) interface{} {
// variable: "#cmd:variable_name"
// Trim the # symbol from the string.
value := strings.TrimLeft(variable, "#")
// Find the first instance of the separator.
idx := strings.Index(value, ":")
if idx == -1 {
return nil
}
// Split the key and variable apart.
cmd := value[0:idx]
vari := value[idx+1:]
// Does the variable_name exist in the vars map.
param, exists := vars[vari]
if !exists {
return nil
}
// What native format do we need.
switch cmd {
case "number":
i, err := strconv.Atoi(param)
if err != nil {
return nil
}
return i
case "string":
return fmt.Sprintf("%q", param)
default:
return nil
}
}