-
Notifications
You must be signed in to change notification settings - Fork 0
/
chunks_example_test.go
71 lines (58 loc) · 1.37 KB
/
chunks_example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2022 go-deeper. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package chunks_test
import (
"errors"
"fmt"
"github.com/go-deeper/chunks"
)
func ExampleSplit() {
slice := []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
sliceChunks := chunks.Split(slice, 9)
fmt.Println("chunks", sliceChunks)
// Output:
// chunks [[1 2 3 4 5] [6 7 8 9 10]]
}
func ExampleSplitFunc() {
slice := []int64{1, 2, 3, 4, 5, 6}
err := chunks.SplitFunc(slice, 5, func(chunk []int64) error {
fmt.Println("chunk", chunk)
return nil
})
fmt.Println("error", err)
// Output:
// chunk [1 2 3]
// chunk [4 5 6]
// error <nil>
}
func ExampleSplitFunc_withError() {
slice := []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
err := chunks.SplitFunc(slice, 4, func(chunk []int64) error {
if chunk[0] > 5 {
return errors.New("some error")
}
fmt.Println("chunk", chunk)
return nil
})
fmt.Println("error", err)
// Output:
// chunk [1 2 3]
// chunk [4 5 6]
// error some error
}
func ExampleSplitFunc_withBreak() {
slice := []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
err := chunks.SplitFunc(slice, 4, func(chunk []int64) error {
if chunk[0] > 5 {
return chunks.ErrBreak
}
fmt.Println("chunk", chunk)
return nil
})
fmt.Println("error", err)
// Output:
// chunk [1 2 3]
// chunk [4 5 6]
// error <nil>
}