Questions tagged [waitgroup]
42 questions
8
votes
2 answers
How to get the number of goroutines associated with a WaitGroup?
Let’s say I use a WaitGroup to make the main thread of an application wait until all the goroutines I have launched from said main have completed.
Is there a safe, straightforward, way to assess at any point in time how many goroutines associated…

Serge Hulne
- 594
- 5
- 17
3
votes
1 answer
loading docker image fails
I am using golang, docker client to load a docker image in .tar format.
func loadImageFromTar(cli *client.Client, tarFilePath string) (string, error) {
// Read tar file
tarFile, err := os.Open(tarFilePath)
if err != nil {
return…

BhanuKiran
- 2,631
- 3
- 20
- 36
2
votes
1 answer
Deadlock - all goroutines are asleep (even when using wait groups)
I am learning go concurrency and I want two go routines to keep talking to each other while passing along updated values via channels. One add 2 to the number while the other subtracts 1. The code and the output are given below:
What's wrong with…

Om Gupta
- 192
- 2
- 8
2
votes
1 answer
How can I write a function which accept 2 functions (which return structs) and runs them concurrently?
Within a golang package I am writing, I often have to make 2 HTTP requests to get the data I require.
My package contains client functions, which usually have 0 arguments and return a struct and an error.
func (c Client) GetProduct() (*Product,…

slatermorgan
- 145
- 6
2
votes
1 answer
Golang 'defer' causing delay in sending(receiving) API response
I have created an API, which after processing the request sends a response and starts a background goroutine which logs some messages. I have used 'defer' to execute goroutine after the API request has been handled.
Following are the code…

qa805542
- 65
- 4
2
votes
4 answers
Check if all goroutines have finished without using wg.Wait()
Let's say I have a function IsAPrimaryColour() which works by calling three other functions IsRed(), IsGreen() and IsBlue(). Since the three functions are quite independent of one another, they can run concurrently. The return conditions are:
If…
user15558657
1
vote
2 answers
What is the cause of the deadlock in my Go code using WaitGroups and Buffered Channels?
WaitGroups, Buffered Channels, and Deadlocks
I have this bit of code which results in a deadlock and I'm not certain why. I have tried using mutex locking in a few different places, closing channels in and outside of separate go routines, but the…

salman b
- 21
- 3
1
vote
1 answer
How to (or should I) prevent Go WaitGroup calls Add() during Wait()?
Let's say I have 2 goroutines, one running in another, each needs a waitgroup to prevent goroutine leak. Here is the code:
func A(wg *sync.WaitGroup) {
defer wg.Done()
// do something
}
func B(wg *sync.WaitGroup) {
defer wg.Done()
…

David M
- 433
- 1
- 4
- 10
1
vote
1 answer
Goroutine Kafka Consumers
I currently have a program that creates a workergroup of size 1, which then calls startworker:
package main
import (
"db_write_consumer/db"
"db_write_consumer/worker"
"os"
"os/signal"
"syscall"
)
func main() {
sigchan :=…

ILoveCliques
- 135
- 1
- 11
1
vote
1 answer
How to pass WaitGroup to a sequential function call?
I have a function which can be called sequentially or concurrently in separate goroutine.
I want to ensure that function is executed completely before main goroutine finishes, so I am passing *sync.WaitGroup argument to the function. Now, at some…

Sonali D Bhavsar
- 155
- 8
1
vote
1 answer
In Go, How to concurrently append to slice with filtering while preserving the order and no data race
Hello here's my sample go playground https://go.dev/play/p/dUUsf6OXt1x
input := []int{1, 2, 3, 4, 5, 6, 7, 8}
wg := sync.WaitGroup{}
var result []int
for i, num := range input {
wg.Add(1)
go func(num, i int) {
if num%2 == 0 {
…

yssachan
- 109
- 7
1
vote
1 answer
In Go, how do we apply concurrency calls while preserving the order of the list?
To give you context,
The variable elementInput is dynamic. I do not know the exact length of it.
It can be 10, 5, or etc.
The *Element channel type is struct
My example is working. But my problem is this implementation is still synchronized, because…

yssachan
- 109
- 7
1
vote
2 answers
Populating an array using Channels in a WaitGroup routine
I want to populate an array of arrays inside a subroutine. I am trying to do this using a channel. I am learning go, so unclear if this is the right way, so please correct me if I am going in the wrong direction, but my code never returns. What am I…

Ufder
- 527
- 4
- 20
1
vote
3 answers
How to efficiently parallelize array list and control the parallelism?
I have a resourceId array which I need loop in parallel. And generate URL for each resource and then put inside a map which is key (resourcId) and value is url.
I got below code which does the job but I am not sure if this is the right way to do it.…

AndyP
- 527
- 1
- 14
- 36
1
vote
1 answer
calling wait group done right after go routine starts?
https://go.dev/play/p/YVYRWSgcp4u
I'm seeing this code in "Concurrency in Go Tools and Techniques for Developers", where it's mentioned about the usage for broadcast, the context is to use broadcast to wake three goroutings.
package main
import (
…

Jack Yao
- 38
- 5