summaryrefslogtreecommitdiff
path: root/src/io
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2022-06-03 16:00:16 -0400
committerGopher Robot <gobot@golang.org>2022-06-04 14:00:38 +0000
commitf8a53df314e4af8cd350eedb0dae77d4c4fc30d0 (patch)
tree87799bdeda270fe74bc07380d57c94b9ea60b07f /src/io
parent21f05284c79c3e823169c62d189826f735006d43 (diff)
downloadgo-git-f8a53df314e4af8cd350eedb0dae77d4c4fc30d0.tar.gz
io: revert: add an Err field to LimitedReader
We are having a hard time deciding the exact semantics of the Err field, and we need to ship the beta. So revert the Err field change; it can wait for Go 1.20. For #51115. This reverts CL 396215. Change-Id: I7719386567d3da10a614058a11f19dbccf304b4d Reviewed-on: https://go-review.googlesource.com/c/go/+/410133 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Bryan Mills <bcmills@google.com> Auto-Submit: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src/io')
-rw-r--r--src/io/example_test.go14
-rw-r--r--src/io/io.go16
2 files changed, 5 insertions, 25 deletions
diff --git a/src/io/example_test.go b/src/io/example_test.go
index e4b20bd981..419e449982 100644
--- a/src/io/example_test.go
+++ b/src/io/example_test.go
@@ -6,7 +6,6 @@ package io_test
import (
"bytes"
- "errors"
"fmt"
"io"
"log"
@@ -284,16 +283,3 @@ func ExampleReadAll() {
// Output:
// Go is a general-purpose language designed with systems programming in mind.
}
-
-func ExampleLimitedReader() {
- r := strings.NewReader("some io.Reader stream to be read\n")
- sentinel := errors.New("reached read limit")
- lr := &io.LimitedReader{R: r, N: 4, Err: sentinel}
-
- if _, err := io.Copy(os.Stdout, lr); err != sentinel {
- log.Fatal(err)
- }
-
- // Output:
- // some
-}
diff --git a/src/io/io.go b/src/io/io.go
index 830779e79d..db88125f50 100644
--- a/src/io/io.go
+++ b/src/io/io.go
@@ -455,26 +455,20 @@ func copyBuffer(dst Writer, src Reader, buf []byte) (written int64, err error) {
// LimitReader returns a Reader that reads from r
// but stops with EOF after n bytes.
// The underlying implementation is a *LimitedReader.
-func LimitReader(r Reader, n int64) Reader { return &LimitedReader{r, n, nil} }
+func LimitReader(r Reader, n int64) Reader { return &LimitedReader{r, n} }
// A LimitedReader reads from R but limits the amount of
// data returned to just N bytes. Each call to Read
// updates N to reflect the new amount remaining.
-// Read returns Err when N <= 0.
-// If Err is nil, it returns EOF instead.
+// Read returns EOF when N <= 0 or when the underlying R returns EOF.
type LimitedReader struct {
- R Reader // underlying reader
- N int64 // max bytes remaining
- Err error // error to return on reaching the limit
+ R Reader // underlying reader
+ N int64 // max bytes remaining
}
func (l *LimitedReader) Read(p []byte) (n int, err error) {
if l.N <= 0 {
- err := l.Err
- if err == nil {
- err = EOF
- }
- return 0, err
+ return 0, EOF
}
if int64(len(p)) > l.N {
p = p[0:l.N]