MCPcopy
hub / github.com/adonovan/gopl.io / display

Function display

ch12/display/display.go:54–88  ·  view source on GitHub ↗

!+display

(path string, v reflect.Value)

Source from the content-addressed store, hash-verified

52
53//!+display
54func display(path string, v reflect.Value) {
55 switch v.Kind() {
56 case reflect.Invalid:
57 fmt.Printf("%s = invalid\n", path)
58 case reflect.Slice, reflect.Array:
59 for i := 0; i < v.Len(); i++ {
60 display(fmt.Sprintf("%s[%d]", path, i), v.Index(i))
61 }
62 case reflect.Struct:
63 for i := 0; i < v.NumField(); i++ {
64 fieldPath := fmt.Sprintf("%s.%s", path, v.Type().Field(i).Name)
65 display(fieldPath, v.Field(i))
66 }
67 case reflect.Map:
68 for _, key := range v.MapKeys() {
69 display(fmt.Sprintf("%s[%s]", path,
70 formatAtom(key)), v.MapIndex(key))
71 }
72 case reflect.Ptr:
73 if v.IsNil() {
74 fmt.Printf("%s = nil\n", path)
75 } else {
76 display(fmt.Sprintf("(*%s)", path), v.Elem())
77 }
78 case reflect.Interface:
79 if v.IsNil() {
80 fmt.Printf("%s = nil\n", path)
81 } else {
82 fmt.Printf("%s.type = %s\n", path, v.Elem().Type())
83 display(path+".value", v.Elem())
84 }
85 default: // basic types, channels, funcs
86 fmt.Printf("%s = %s\n", path, formatAtom(v))
87 }
88}
89
90//!-display

Callers 1

DisplayFunction · 0.85

Calls 2

formatAtomFunction · 0.70
LenMethod · 0.45

Tested by

no test coverage detected