August 2019
Beginner to intermediate
798 pages
17h 2m
English
In this section, you will see what happens if you forget to unlock a sync.Mutex. You will do this using the Go code of forgetMutex.go, which will be presented in two parts.
The first part of forgetMutex.go is shown in the following Go code:
package main
import (
"fmt"
"sync"
)
var m sync.Mutex
func function() {
m.Lock()
fmt.Println("Locked!")
}
All of the problems in this program are caused because the developer forgot to release the lock to m sync.Mutex. However, if your program is going to call function() only once, then everything will look just fine.
The second part of forgetMutex.go is as follows:
func main() { var w sync.WaitGroup go func() { defer w.Done() function() }() w.Add(1) go func() ...Read now
Unlock full access