Forked from VojtechVitek/slice-batch-in-parallel.go
          
        
    
          Created
          February 3, 2020 17:01 
        
      - 
      
 - 
        
Save figassis/85ef6f1fa333adda88b6f3cf1029aa01 to your computer and use it in GitHub Desktop.  
    Golang - Loop over slice in batches (run something in parallel on a sub-slice)
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | package main | |
| import "fmt" | |
| func main() { | |
| slice := make([]int, 159) | |
| // Split the slice into batches of 20 items. | |
| batch := 20 | |
| for i := 0; i < len(slice); i += batch { | |
| j := i + batch | |
| if j > len(slice) { | |
| j = len(slice) | |
| } | |
| fmt.Println(slice[i:j]) // Process the batch. | |
| } | |
| // Try it at https://play.golang.org/p/mgd814w8xx6 | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment