gochunks

package module
v0.0.0-...-1e64f9f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 18, 2023 License: GPL-3.0 Imports: 0 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func MakeConcurrencyChunks

func MakeConcurrencyChunks(maxLength int, concurrency int) [][]int

MakeConcurrencyChunks will return chunks of ids. The basic use case is to fill in a list concurrently in a concise way. Indexes will go (no pun intended) from 0 to maxLength-1 and be broken down between chunks of concurrency or less size.

Example
numbersToHalves := []int{4, 10, 30, 200, 3090}
halvedNumbers := make([]int, 5)

concurrentIdsChunks := MakeConcurrencyChunks(5, 2)

// iterate over chunks with their own waitgroup synchronously
for _, cChunk := range concurrentIdsChunks {
	var wg sync.WaitGroup
	wg.Add(len(cChunk))
	// for each chunk conrrent task pop a worker and perform task
	for _, taskId := range cChunk {
		go func(i int) {
			halvedNumbers[i] = numbersToHalves[i] >> 1
			wg.Done()
		}(taskId)
	}
	wg.Wait()
}
fmt.Println(halvedNumbers)
Output:

{2, 5, 15, 100, 1545}

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL