Skip to content

Commit 0796ec8

Browse files
committed
io: add doc/next file and remove errors.Is from example
1 parent 9b6b14c commit 0796ec8

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<!-- go.dev/issue/51115 -->
2+
The new [LimitedReader.Err] field allows returning a custom error
3+
when the read limit is exceeded. When nil (the default), [LimitedReader.Read]
4+
returns [EOF] when the limit is reached, maintaining backward compatibility.

src/io/example_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,15 @@ func ExampleLimitedReader_Err() {
132132
if err != nil {
133133
log.Fatal(err)
134134
}
135-
fmt.Printf("read %d bytes: %q\n", n, buf[:n])
135+
fmt.Printf("%d; %q\n", n, buf[:n])
136136

137137
// try to read more and get the custom error
138138
n, err = lr.Read(buf)
139-
if errors.Is(err, sentinel) {
140-
fmt.Println("error:", err)
141-
}
139+
fmt.Printf("%d; error: %v\n", n, err)
142140

143141
// Output:
144-
// read 4 bytes: "some"
145-
// error: read limit reached
142+
// 4; "some"
143+
// 0; error: read limit reached
146144
}
147145

148146
func ExampleMultiReader() {

src/io/io.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -556,10 +556,8 @@ func (s *SectionReader) Read(p []byte) (n int, err error) {
556556
return
557557
}
558558

559-
var (
560-
errWhence = errors.New("Seek: invalid whence")
561-
errOffset = errors.New("Seek: invalid offset")
562-
)
559+
var errWhence = errors.New("Seek: invalid whence")
560+
var errOffset = errors.New("Seek: invalid offset")
563561

564562
func (s *SectionReader) Seek(offset int64, whence int) (int64, error) {
565563
switch whence {

0 commit comments

Comments
 (0)