github.com/kaydxh/golang@v0.0.131/pkg/pool/task/batch_process.go (about) 1 package task 2 3 import "fmt" 4 5 func BatchProcess[T any](data []T, batchSize int, f func(d []T) error) error { 6 if batchSize <= 0 { 7 return fmt.Errorf("invalid batchSize %d", batchSize) 8 } 9 10 if f == nil { 11 return fmt.Errorf("porcess func is nil") 12 } 13 14 for start, end := 0, 0; start < len(data); start = end { 15 end = start + batchSize 16 if end > len(data) { 17 end = len(data) 18 } 19 batch := data[start:end] 20 err := f(batch) 21 if err != nil { 22 return err 23 } 24 } 25 26 return nil 27 }