github.com/tunabay/go-bitarray@v1.3.1/bitarray_iterate_example_test.go (about) 1 // Copyright (c) 2021 Hirotsuna Mizuno. All rights reserved. 2 // Use of this source code is governed by the MIT license that can be found in 3 // the LICENSE file. 4 5 package bitarray_test 6 7 import ( 8 "fmt" 9 10 "github.com/tunabay/go-bitarray" 11 ) 12 13 func ExampleBitArray_Iterate() { 14 ba := bitarray.MustParse("1111 0101 0000") 15 16 fn := func(i, b int) error { 17 fmt.Printf("%d: %d\n", i, b) 18 if i == 10 { 19 return bitarray.BreakIteration 20 } 21 return nil 22 } 23 if err := ba.Iterate(fn); err != nil { 24 panic(err) 25 } 26 27 // Output: 28 // 0: 1 29 // 1: 1 30 // 2: 1 31 // 3: 1 32 // 4: 0 33 // 5: 1 34 // 6: 0 35 // 7: 1 36 // 8: 0 37 // 9: 0 38 // 10: 0 39 } 40 41 func ExampleBitArray_Iterate_error() { 42 ba := bitarray.MustParse("000010") 43 44 fn := func(i, b int) error { 45 if b == 1 { 46 return fmt.Errorf("unexpected bit 1 at %d", i) 47 } 48 fmt.Printf("%d: %d\n", i, b) 49 return nil 50 } 51 if err := ba.Iterate(fn); err != nil { 52 fmt.Printf("got error: %s\n", err) 53 } 54 55 // Output: 56 // 0: 0 57 // 1: 0 58 // 2: 0 59 // 3: 0 60 // got error: unexpected bit 1 at 4 61 }