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

Function formatAtom

ch12/display/display.go:26–51  ·  view source on GitHub ↗

!-Display formatAtom formats a value without inspecting its internal structure. It is a copy of the the function in gopl.io/ch11/format.

(v reflect.Value)

Source from the content-addressed store, hash-verified

24// formatAtom formats a value without inspecting its internal structure.
25// It is a copy of the the function in gopl.io/ch11/format.
26func formatAtom(v reflect.Value) string {
27 switch v.Kind() {
28 case reflect.Invalid:
29 return "invalid"
30 case reflect.Int, reflect.Int8, reflect.Int16,
31 reflect.Int32, reflect.Int64:
32 return strconv.FormatInt(v.Int(), 10)
33 case reflect.Uint, reflect.Uint8, reflect.Uint16,
34 reflect.Uint32, reflect.Uint64, reflect.Uintptr:
35 return strconv.FormatUint(v.Uint(), 10)
36 // ...floating-point and complex cases omitted for brevity...
37 case reflect.Bool:
38 if v.Bool() {
39 return "true"
40 }
41 return "false"
42 case reflect.String:
43 return strconv.Quote(v.String())
44 case reflect.Chan, reflect.Func, reflect.Ptr,
45 reflect.Slice, reflect.Map:
46 return v.Type().String() + " 0x" +
47 strconv.FormatUint(uint64(v.Pointer()), 16)
48 default: // reflect.Array, reflect.Struct, reflect.Interface
49 return v.Type().String() + " value"
50 }
51}
52
53//!+display
54func display(path string, v reflect.Value) {

Callers 1

displayFunction · 0.70

Calls 1

StringMethod · 0.45

Tested by

no test coverage detected