-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.go
More file actions
58 lines (45 loc) · 1.87 KB
/
Copy pathexample.go
File metadata and controls
58 lines (45 loc) · 1.87 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
package main
import (
"fmt"
gp "github.com/goiste/goparser"
)
func main() {
p, err := gp.New("example_code.go")
if err != nil {
panic(err)
}
boolValues := gp.GetBasicValues[bool](p, "parser")
for _, v := range boolValues {
fmt.Printf("name: %s; value: %t\n", v.Name, v.Value) // name: boolValue; value: true
}
stringValues := gp.GetBasicValues[string](p, "parser", "parser:str")
for _, v := range stringValues {
fmt.Printf("name: %s; value: %q\n", v.Name, v.Value) // name: stringValue; value: "3"
}
floatValues := gp.GetBasicValues[float64](p, "parser")
for _, v := range floatValues {
fmt.Printf("name: %s; value: %.02f\n", v.Name, v.Value) // name: float64Value; value: 3.14
}
floatSliceValues := gp.GetSliceValues[float64](p, "parser")
for _, v := range floatSliceValues {
fmt.Printf("name: %s; values: %v\n", v.Name, v.Value) // name: float64SliceValue; values: [3.14 0.42]
}
stringSliceValues := gp.GetSliceValues[string](p, "parser", "parser:str")
for _, v := range stringSliceValues {
fmt.Printf("name: %s; values: %v\n", v.Name, v.Value) // name: stringSliceValue; values: [a b c]
}
stringMapValues := gp.GetMapValues[string, string](p, "parser", "parser:str")
for _, v := range stringMapValues {
fmt.Printf("name: %s; values: %+v\n", v.Name, v.Value) // name: stringToStringMapValue; values: map[a:1 b:2]
}
floatMapValues := gp.GetMapValues[int64, float64](p, "parser")
for _, v := range floatMapValues {
fmt.Printf("name: %s; values: %+v\n", v.Name, v.Value) // name: intToFloat64MapValue; values: map[3:3.14 17:42]
}
nms := gp.GetFuncNames(p, "LocalStruct", "Context", "string")
fmt.Printf("%v\n", nms) // [usefulFunc1 usefulFunc2 usefulFunc3]
nms = gp.GetFuncNames(p, "LocalStruct", "Context")
fmt.Printf("%v\n", nms) // [usefulFunc1 usefulFunc2 usefulFunc3]
nms = gp.GetFuncNames(p, "", "Context")
fmt.Printf("%v\n", nms) // [usefulFunc4]
}