Parcourir la source

Detect nil slices and print them as <nil>.

This commit modifies the code to detect nil slices and display them as
<nil> (as opposed to simply empty slices).  For most instances a nil slice
can be treated the same as an empty slice, but there is a difference and
things like reflect.DeepEqual notice.  This change makes it clear whether
the type in question is a nil slice or an empty slice.
Dave Collins il y a 12 ans
Parent
commit
7ea732c827
2 fichiers modifiés avec 16 ajouts et 2 suppressions
  1. 8 1
      spew/dump.go
  2. 8 1
      spew/format.go

+ 8 - 1
spew/dump.go

@@ -245,7 +245,14 @@ func (d *dumpState) dump(v reflect.Value) {
 	case reflect.Complex128:
 		printComplex(d.w, v.Complex(), 64)
 
-	case reflect.Array, reflect.Slice:
+	case reflect.Slice:
+		if v.IsNil() {
+			d.w.Write(nilAngleBytes)
+			break
+		}
+		fallthrough
+
+	case reflect.Array:
 		d.w.Write(openBraceNewlineBytes)
 		d.depth++
 		if (d.cs.MaxDepth != 0) && (d.depth > d.cs.MaxDepth) {

+ 8 - 1
spew/format.go

@@ -256,7 +256,14 @@ func (f *formatState) format(v reflect.Value) {
 	case reflect.Complex128:
 		printComplex(f.fs, v.Complex(), 64)
 
-	case reflect.Array, reflect.Slice:
+	case reflect.Slice:
+		if v.IsNil() {
+			f.fs.Write(nilAngleBytes)
+			break
+		}
+		fallthrough
+
+	case reflect.Array:
 		f.fs.Write(openBracketBytes)
 		f.depth++
 		if (f.cs.MaxDepth != 0) && (f.depth > f.cs.MaxDepth) {