From 3fdaf5cea8d23107b993d363e98992fa529dd713 Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Mon, 5 May 2014 22:09:56 -0500 Subject: Dump non-zero len and cap for applicable types. Closes #16. --- spew/dump.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'spew/dump.go') diff --git a/spew/dump.go b/spew/dump.go index 3d57306..02d4c9d 100644 --- a/spew/dump.go +++ b/spew/dump.go @@ -270,6 +270,32 @@ func (d *dumpState) dump(v reflect.Value) { } d.ignoreNextType = false + // Display length and capacity if the built-in len and cap functions + // work with the value's kind and the len/cap itself is non-zero. + valueLen, valueCap := 0, 0 + switch v.Kind() { + case reflect.Array, reflect.Slice, reflect.Chan: + valueLen, valueCap = v.Len(), v.Cap() + case reflect.Map, reflect.String: + valueLen = v.Len() + } + if valueLen != 0 || valueCap != 0 { + d.w.Write(openParenBytes) + if valueLen != 0 { + d.w.Write(lenEqualsBytes) + printInt(d.w, int64(valueLen), 10) + } + if valueCap != 0 { + if valueLen != 0 { + d.w.Write(spaceBytes) + } + d.w.Write(capEqualsBytes) + printInt(d.w, int64(valueCap), 10) + } + d.w.Write(closeParenBytes) + d.w.Write(spaceBytes) + } + // Call Stringer/error interfaces if they exist and the handle methods flag // is enabled if !d.cs.DisableMethods { -- cgit v1.2.3