Skip to content

Commit 36280c0

Browse files
authored
Merge pull request #800 from devlights/add-section-reader-example
2 parents a8bd058 + 037f842 commit 36280c0

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

examples/basic/ioop/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
| multiwrite.go | ioop_multiwrite | io.MultiWriterを利用してgzip圧縮しながらCRCチェックサムも算出するサンプルです. |
1010
| multiread.go | ioop_multiread | io.MultiReaderを利用して複数のファイルを一気に読み込むサンプルです。 |
1111
| teeread.go | ioop_tee_read | io.TeeReader を利用したサンプルです。 |
12+
| sectionread.go | ioop_section_read | io.SectionReader を利用したサンプルです。 |

examples/basic/ioop/examples.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ func (r *register) Regist(m mapping.ExampleMapping) {
2020
m["ioop_multiwrite"] = MultiWrite
2121
m["ioop_multireader"] = MultiRead
2222
m["ioop_tee_read"] = TeeRead
23+
m["ioop_section_read"] = SectionRead
2324
}

examples/basic/ioop/sectionread.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package ioop
2+
3+
import (
4+
"bytes"
5+
"io"
6+
"strings"
7+
8+
"github.com/devlights/gomy/output"
9+
)
10+
11+
// SectionRead は、io.SectionReader を利用したサンプルです。
12+
//
13+
// io.SectionReader は、指定した範囲のデータを読み込んでくれる特殊ストリーム。
14+
//
15+
// > SectionReader implements Read, Seek, and ReadAt on a section of an underlying ReaderAt.
16+
//
17+
// > SectionReaderは、Read、Seek、ReadAtを実装しています。
18+
//
19+
// # REFERENCES
20+
// - https://pkg.go.dev/io@go1.22.2#SectionReader
21+
// - https://cs.opensource.google/go/go/+/refs/tags/go1.22.2:src/io/io.go;l=501
22+
func SectionRead() error {
23+
var (
24+
r = strings.NewReader("helloworld こんにちは世界")
25+
secR = io.NewSectionReader(r, 11, 15)
26+
buf = new(bytes.Buffer)
27+
err error
28+
)
29+
30+
_, err = io.Copy(buf, secR)
31+
if err != nil {
32+
return err
33+
}
34+
35+
output.Stdoutl("[secR]", buf.String())
36+
37+
return nil
38+
39+
/*
40+
$ task
41+
task: [build] go build .
42+
task: [run] ./try-golang -onetime
43+
44+
ENTER EXAMPLE NAME: ioop_section_read
45+
46+
[Name] "ioop_section_read"
47+
[secR] こんにちは
48+
49+
50+
[Elapsed] 24.97µs
51+
*/
52+
53+
}

0 commit comments

Comments
 (0)