[go] How can i define in this code- dead locks

func main() {
//var sharedlock sync.Mutex
var wg sync.WaitGroup

defer wg.Done()

worker := func() {

	for i := 0; i < 100000; i++ {
		fmt.Printf("%v\n", i)

	}

}

go worker()
wg.Add(1)
wg.Wait()

}

I get this :
all goroutines are asleep - deadlock!

defer wg.Done() will be called when main function ends. I suppose you forget to put Done function into the worker function.
you are calling Add(1), and calling Wait() without calling Done()

func main() {
//var sharedlock sync.Mutex
var wg sync.WaitGroup
worker := func() {
    defer wg.Done()
	for i := 0; i < 100000; i++ {
        //pass
	}
}

go worker()
wg.Add(1)
wg.Wait()

}

2 Likes

Thak you! you best