I
I
impressive172021-04-28 10:34:03
go
impressive17, 2021-04-28 10:34:03

How to run n goroutines in golang?

I need to break the start-end time interval into 10-hour intervals and run the process (from, to) method in the goroutine for each. That is, depending on the length of the start-end time interval, a different number of goroutines are launched. I do it through errgroup.WithContext(ctx)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
1
12rbah, 2021-04-28
@12rbah

To be honest, I didn’t quite understand what the problem is, just run the goroutines in a loop that will have the number of iterations depending on the start-end.

U
uvelichitel, 2021-04-28
@uvelichitel

For example like this

//На N промежутков
step:=(end-start)/N
for n := 0; n < N; n++ {
    from := start + n*step
    to := from + step
    group.Go(func() error{ 
         return process(from, to)})
     })
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question