I am new to the Golang, please bear me and kindly clarify my query.
what I understood from wg.Done() call, this is an indication to WaitGroip that my goroutine is done so that wait call by WaitGroup will end, let's consider a scenario where we have only 1 goroutine running, So generally we use defer keyword for wg.Done() so it will get called at the end of the goroutine block.
I came across below code snippet, where wg.Done() it's being called in the midway, I am confused why it's being called here and also when I practiced code using wg.Done() in the midway, code after that it's not been called, So with that in mind I am confused in the below codebase wg.Done() is being called in the mid-way.
func startNetworkDaemon() *sync.WaitGroup {
var wg sync.WaitGroup
wg.Add(1)
go func() {
connPool := warmServiceConnCache()
server, err := net.Listen("tcp", "localhost:8080")
if err != nil {
log.Fatalf("cannot listen: %v", err)
}
defer server.Close()
wg.Done()
for {
conn, err := server.Accept()
if err != nil {
log.Printf("cannot accept connection: %v", err)
continue
}
svcConn := connPool.Get()
fmt.Fprintln(conn, "")
connPool.Put(svcConn)
conn.Close()
}
}()
return &wg
}
Another code sample as well, in the below code if add defer keyword to the wg.Done() method, I am getting a deadlock error. Can someone explain the reason for this...
func usingBroadcast() {
beeper := sync.NewCond(&sync.Mutex{})
var wg sync.WaitGroup
wg.Add(1)
miniFunc(func() {
fmt.Println("mini1")
wg.Done()
}, beeper)
beeper.Broadcast()
wg.Wait()
}
func miniFunc(fn func(), beeper *sync.Cond) {
var wg sync.WaitGroup
wg.Add(1)
go func() {
wg.Done()
beeper.L.Lock()
fmt.Println("i am waiting")
beeper.Wait()
beeper.L.Unlock()
fn()
}()
wg.Wait()
}