1
0
mirror of https://github.com/golang/go synced 2024-11-22 06:04:39 -07:00

return "<nil>" when calling String() on a nil bytes.Buffer.

R=rsc
CC=go-dev
http://go/go-review/1016005
This commit is contained in:
Rob Pike 2009-10-31 13:28:22 -07:00
parent aa0c811317
commit 63e668d2ad
2 changed files with 13 additions and 1 deletions

View File

@ -42,8 +42,12 @@ func (b *Buffer) Bytes() []byte {
} }
// String returns the contents of the unread portion of the buffer // String returns the contents of the unread portion of the buffer
// as a string. // as a string. If the Buffer is a nil pointer, it returns "<nil>".
func (b *Buffer) String() string { func (b *Buffer) String() string {
if b == nil {
// Special case, useful in debugging.
return "<nil>"
}
return string(b.buf[b.off : len(b.buf)]); return string(b.buf[b.off : len(b.buf)]);
} }

View File

@ -232,3 +232,11 @@ func TestMixedReadsAndWrites(t *testing.T) {
} }
empty(t, "TestMixedReadsAndWrites (2)", &buf, s, make([]byte, buf.Len())); empty(t, "TestMixedReadsAndWrites (2)", &buf, s, make([]byte, buf.Len()));
} }
func TestNil(t *testing.T) {
var b *Buffer;
if b.String() != "<nil>" {
t.Error("expcted <nil>; got %q", b.String());
}
}