Skip to content

Commit 3e7a1f6

Browse files
committed
Add byteop/go124_bytes_lines.go
1 parent 094b501 commit 3e7a1f6

File tree

4 files changed

+69
-5
lines changed

4 files changed

+69
-5
lines changed

examples/basic/byteop/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
このディレクトリには以下のサンプルがあります。
44

5-
| file | example name | note |
6-
| ------------------------ | ---------------------------- | --------------------------------------------- |
7-
| reader_from_byteslice.go | byteop_reader_from_byteslice | []byte から io.Reader を生成するサンプルです. |
8-
| using_repeat.go | byteop_using_repeat | bytes.Repeat() のサンプルです |
5+
| file | example name | note |
6+
| ------------------------ | ---------------------------- | -------------------------------------------------- |
7+
| reader_from_byteslice.go | byteop_reader_from_byteslice | []byte から io.Reader を生成するサンプルです. |
8+
| using_repeat.go | byteop_using_repeat | bytes.Repeat() のサンプルです |
9+
| cut_prefix_suffix.go | byteop_cut_prefix_suffix | bytes.{CutPrefix,CutSuffix} のサンプルです. |
10+
| go124_bytes_lines.go | byteop_go124_bytes_lines | Go 1.24 で追加された bytes.Lines() のサンプルです. |

examples/basic/byteop/examples.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ func (r *register) Regist(m mapping.ExampleMapping) {
1616
m["byteop_reader_from_byteslice"] = ReaderFromByteSlice
1717
m["byteop_cut_prefix_suffix"] = CutPrefixSuffix
1818
m["byteop_using_repeat"] = UsingRepeat
19+
m["byteop_go124_bytes_lines"] = Go124BytesLines
1920
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package byteop
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"iter"
7+
"os"
8+
)
9+
10+
// Go124BytesLines は、Go 1.24 で追加された bytes.Lines() のサンプルです.
11+
//
12+
// Go 1.23 で追加されたイテレータを返すようになっています。
13+
// bytes.Lines()は、バイト列を '\n' で区切ったデータをイテレータで提供してくれます。
14+
// このデータには '\n' 自身も付いた状態で取得出来るので、その点には注意が必要。
15+
// (fmt.Println()でそのまま出力すると2個改行が入ることになる)
16+
//
17+
// # REFERENCES
18+
// - https://pkg.go.dev/bytes@go1.24.0#Lines
19+
// - https://tip.golang.org/doc/go1.24#bytespkgbytes
20+
// - https://tip.golang.org/doc/go1.23#iterators
21+
func Go124BytesLines() error {
22+
var (
23+
// 最初のN行のみに絞るためのイテレータ関数
24+
firstN = func(n int, lines iter.Seq[[]byte]) iter.Seq[[]byte] {
25+
return func(yield func([]byte) bool) {
26+
count := 0
27+
for line := range lines {
28+
if count >= n || !yield(line) {
29+
return
30+
}
31+
32+
count++
33+
}
34+
}
35+
}
36+
// 前に番号を付与するイテレータ
37+
withLineNum = func(lines iter.Seq[[]byte]) iter.Seq[string] {
38+
return func(yield func(string) bool) {
39+
count := 1
40+
for line := range lines {
41+
if !yield(fmt.Sprintf("%2d: %s", count, line)) {
42+
return
43+
}
44+
45+
count++
46+
}
47+
}
48+
}
49+
50+
data, _ = os.ReadFile("README.md") // ファイルデータを読み出して
51+
lines = bytes.Lines(data) // 行単位にして
52+
first5 = firstN(5, lines) // 先頭5行のみにして
53+
withNum = withLineNum(first5) // 行番号を付与
54+
)
55+
56+
for line := range withNum {
57+
fmt.Printf("%s", line)
58+
}
59+
60+
return nil
61+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/devlights/try-golang
22

3-
go 1.23
3+
go 1.24
44

55
require (
66
github.com/devlights/gomy v0.6.0

0 commit comments

Comments
 (0)