Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ElapsedTime ¶
Types ¶
type Batches ¶
type Batches struct { From int64 To int64 BatchSize uint32 // BatchSize defaults to 1 if is 0. Concurrency uint16 Work func(int64, int64) error Output io.Writer }
func (Batches) Run ¶
Example ¶
err := Batches{ From: 1, To: 307, BatchSize: 100, Work: func(from, to int64) error { fmt.Println(from, to) return nil }, Output: os.Stderr, }.Run() if err != nil { fmt.Println(err) }
Output: 1 100 101 200 201 300 301 307
Example (Concurrently) ¶
var result = [][2]int64{} var lock sync.Mutex err := Batches{ From: 307, To: 7, BatchSize: 100, Concurrency: 5, Work: func(from, to int64) error { time.Sleep(time.Millisecond) lock.Lock() defer lock.Unlock() result = append(result, [2]int64{from, to}) return nil }, Output: os.Stderr, }.Run() if err != nil { fmt.Println(err) return } sort.Slice(result, func(i, j int) bool { return result[i][0] > result[j][0] }) fmt.Println(result)
Output: [[307 208] [207 108] [107 8] [7 7]]
Example (ConcurrentlyWithError) ¶
err := Batches{ From: 1, To: 7, Concurrency: 2, Work: func(from, to int64) error { if from == 4 { return errors.New("error") } return nil }, Output: os.Stderr, }.Run() if err != nil { fmt.Println(err) }
Output: error
Example (ConcurrentlyWithError2) ¶
err := Batches{ From: 1, To: 7, Concurrency: 2, Work: func(from, to int64) error { if from == 4 { return errors.New("error") } time.Sleep(10 * time.Millisecond) return nil }, Output: os.Stderr, }.Run() if err != nil { fmt.Println(err) }
Output: error
Example (Desc) ¶
err := Batches{ From: 307, To: 7, BatchSize: 100, Work: func(from, to int64) error { fmt.Println(from, to) return nil }, Output: os.Stderr, }.Run() if err != nil { fmt.Println(err) }
Output: 307 208 207 108 107 8 7 7
Example (OneNumber) ¶
err := Batches{ From: 3, To: 3, Work: func(from, to int64) error { time.Sleep(time.Millisecond) fmt.Println(from, to) return nil }, Output: os.Stderr, }.Run() if err != nil { fmt.Println(err) }
Output: 3 3
Example (OneNumber2) ¶
err := Batches{ From: 3, To: 3, BatchSize: 3, Work: func(from, to int64) error { fmt.Println(from, to) return nil }, }.Run() if err != nil { fmt.Println(err) }
Output: 3 3
Example (SingleBatch) ¶
err := Batches{ From: 3, To: 1, BatchSize: 100, Work: func(from, to int64) error { time.Sleep(time.Second) fmt.Println(from, to) return nil }, Output: os.Stderr, }.Run() if err != nil { fmt.Println(err) }
Output: 3 1
Example (SingleStep) ¶
err := Batches{ From: 3, To: 1, Work: func(from, to int64) error { fmt.Println(from, to) return nil }, Output: os.Stderr, }.Run() if err != nil { fmt.Println(err) }
Output: 3 3 2 2 1 1
Example (WithError) ¶
err := Batches{ From: 1, To: 307, BatchSize: 100, Work: func(from, to int64) error { fmt.Println(from, to) if from == 201 { return errors.New("error") } return nil }, Output: os.Stderr, }.Run() if err != nil { fmt.Println(err) }
Output: 1 100 101 200 201 300 error
Click to show internal directories.
Click to hide internal directories.