-
Notifications
You must be signed in to change notification settings - Fork 1
/
print.go
54 lines (49 loc) · 1.18 KB
/
print.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
package testutil
import (
"fmt"
"reflect"
"strconv"
"github.com/davecgh/go-spew/spew"
)
// ArrayToString converts any array into pretty-print string
// unless UseSpewToPrettyPrintArrays flag signals to use the spew library
//
// Format:
/*
---(%tag)[%arraySize]---
(0) %valueAtIndex0
(1) %valueAtIndex1
(2) %valueAtIndex2
(3) %valueAtIndex3
...
Example call: ArrayToString("array",[5]int{14234, 42, -1, 1000, 5})
Output:
---(array)[5]---
(0) 14234
(1) 42
(2) -1
(3) 1000
(4) 5
*/
func ArrayToString(tag string, iface interface{}) string {
if UseSpewToPrettyPrintArrays {
return "<" + tag + ">:\n" + spew.Sdump(iface)
}
array := reflect.ValueOf(iface)
//if array.Kind() != reflect.Array {
// panic("This is not array: " + fmt.Sprint(iface))
//}
n := array.Len()
prefix := "---(" + tag + ")"
head := prefix + "-[" + strconv.Itoa(n) + "]---"
prefixLen := len(prefix) + 1
result := head + "\n"
for i := 0; i < n; i++ {
e := array.Index(i)
val := fmt.Sprintf("%v", e)
line := fmt.Sprintf("%"+strconv.Itoa(prefixLen)+"s",
"("+strconv.Itoa(i)+") ") + val
result = result + line + "\n"
}
return result
}