-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbitarray_iterate_example_test.go
More file actions
61 lines (52 loc) · 1008 Bytes
/
bitarray_iterate_example_test.go
File metadata and controls
61 lines (52 loc) · 1008 Bytes
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
// Copyright (c) 2021 Hirotsuna Mizuno. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package bitarray_test
import (
"fmt"
"github.com/tunabay/go-bitarray"
)
func ExampleBitArray_Iterate() {
ba := bitarray.MustParse("1111 0101 0000")
fn := func(i, b int) error {
fmt.Printf("%d: %d\n", i, b)
if i == 10 {
return bitarray.BreakIteration
}
return nil
}
if err := ba.Iterate(fn); err != nil {
panic(err)
}
// Output:
// 0: 1
// 1: 1
// 2: 1
// 3: 1
// 4: 0
// 5: 1
// 6: 0
// 7: 1
// 8: 0
// 9: 0
// 10: 0
}
func ExampleBitArray_Iterate_error() {
ba := bitarray.MustParse("000010")
fn := func(i, b int) error {
if b == 1 {
return fmt.Errorf("unexpected bit 1 at %d", i)
}
fmt.Printf("%d: %d\n", i, b)
return nil
}
if err := ba.Iterate(fn); err != nil {
fmt.Printf("got error: %s\n", err)
}
// Output:
// 0: 0
// 1: 0
// 2: 0
// 3: 0
// got error: unexpected bit 1 at 4
}