Skip to content

Instantly share code, notes, and snippets.

@VojtechVitek
Last active December 17, 2024 21:46
Show Gist options
  • Save VojtechVitek/00f8f22b0ab3ec221815fcd78f244edd to your computer and use it in GitHub Desktop.
Save VojtechVitek/00f8f22b0ab3ec221815fcd78f244edd to your computer and use it in GitHub Desktop.

Revisions

  1. VojtechVitek revised this gist Mar 26, 2019. No changes.
  2. VojtechVitek revised this gist Mar 26, 2019. 1 changed file with 10 additions and 8 deletions.
    18 changes: 10 additions & 8 deletions slice-batch-in-parallel.go
    Original file line number Diff line number Diff line change
    @@ -2,17 +2,19 @@ package main

    import "fmt"

    func main() {
    recipients := make([]int, 159)
    func main() {
    slice := make([]int, 159)

    // Send 20 emails in parallel.
    // Split the slice into batches of 20 items.
    batch := 20

    for i := 0; i < len(recipients); i += batch {
    for i := 0; i < len(slice); i += batch {
    j := i + batch
    if j > len(recipients) {
    j = len(recipients)
    if j > len(slice) {
    j = len(slice)
    }
    fmt.Printf("recipients[%v:%v]\n", i, j)
    fmt.Println(slice[i:j]) // Process the batch.
    }
    }

    // Try it at https://play.golang.org/p/mgd814w8xx6
    }
  3. VojtechVitek created this gist Apr 12, 2017.
    18 changes: 18 additions & 0 deletions slice-batch-in-parallel.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    package main

    import "fmt"

    func main() {
    recipients := make([]int, 159)

    // Send 20 emails in parallel.
    batch := 20

    for i := 0; i < len(recipients); i += batch {
    j := i + batch
    if j > len(recipients) {
    j = len(recipients)
    }
    fmt.Printf("recipients[%v:%v]\n", i, j)
    }
    }