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


(ext) #1
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!


(Ali Koyuncu) #2

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()

}


(ext) #3

Thak you! you best