diff options
| author | Brad Fitzpatrick <bradfitz@golang.org> | 2015-01-22 14:15:47 -0800 |
|---|---|---|
| committer | Brad Fitzpatrick <bradfitz@golang.org> | 2015-04-06 08:53:47 +0000 |
| commit | c264c87335ff4b3111d43f830dbe37eac1509f2e (patch) | |
| tree | 72a631779b376ae0ec4b584453d4b07ba897bf02 /src/strings/reader_test.go | |
| parent | c50a8416c851ab6897f9efcd932ac26b5862dcad (diff) | |
| download | go-c264c87335ff4b3111d43f830dbe37eac1509f2e.tar.xz | |
bytes, strings: add Reader.Size methods
As noted on recently on golang-nuts, there's currently no way to know
the total size of a strings.Reader or bytes.Reader when using ReadAt
on them. Most callers resort to wrapping it in an io.SectionReader to
retain that information.
The SizeReaderAt abstraction (an io.ReaderAt with a Size() int64
method) has proven useful as a way of expressing a concurrency-safe
read-only number of bytes.
As one example, see http://talks.golang.org/2013/oscon-dl.slide#49 and
the rest of that presentation for its use in dl.google.com.
SizeReaderAt is also used in the open source google-api-go-client, and
within Google's internal codebase, where it exists in a public package
created in 2013 with the package comment: "These may migrate to the
standard library after we have enough experience with their feel."
I'm still as happy with the SizeReaderAt abstraction and its
composabilty as I was in 2013, so I'd like to make these two Readers
also be SizeReaderAts.
Fixes #9667
Change-Id: Ie6f145ada419dd116280472d8c029f046d5edf70
Reviewed-on: https://go-review.googlesource.com/3199
Reviewed-by: Andrew Gerrand <adg@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Diffstat (limited to 'src/strings/reader_test.go')
| -rw-r--r-- | src/strings/reader_test.go | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/src/strings/reader_test.go b/src/strings/reader_test.go index bee90eb258..5003a37be4 100644 --- a/src/strings/reader_test.go +++ b/src/strings/reader_test.go @@ -8,6 +8,7 @@ import ( "bytes" "fmt" "io" + "io/ioutil" "os" "strings" "sync" @@ -157,3 +158,15 @@ func TestWriteTo(t *testing.T) { } } } + +// tests that Len is affected by reads, but Size is not. +func TestReaderLenSize(t *testing.T) { + r := strings.NewReader("abc") + io.CopyN(ioutil.Discard, r, 1) + if r.Len() != 2 { + t.Errorf("Len = %d; want 2", r.Len()) + } + if r.Size() != 3 { + t.Errorf("Size = %d; want 3", r.Size()) + } +} |
