Complete the code to start a new goroutine.
go [1]()fmt.Println directly after go without calling a function.main function as a goroutine.time.Sleep without parentheses.The go keyword starts a new goroutine running the function sayHello.
Complete the code to wait for a goroutine to finish using a channel.
done := make(chan bool)
go func() {
fmt.Println("Working...")
done [1] true
}()
<-done<- on the left side incorrectly.To send a value to a channel, use done <- true.
Fix the error in the code to correctly launch multiple goroutines.
for i := 0; i < 3; i++ { go func() { [1] fmt.Println(i) }() }
i with a type inside the loop incorrectly.Passing i as a parameter inside the goroutine fixes the closure capturing issue.
Fill both blanks to create a buffered channel and send a value.
ch := make(chan int, [1]) ch [2] 5
A buffered channel of size 1 is created, then the value 5 is sent using ch <- 5.
Fill all three blanks to create a map with word lengths using concurrency.
words := []string{"go", "code", "fun"}
lengths := make(map[string]int)
var mu sync.Mutex
for _, word := range words {
go func(w string) {
mu.Lock()
lengths[[1]] = len([2])
mu.Unlock()
}([3])
}word directly inside the goroutine causes race conditions.The goroutine uses the parameter w to safely update the map with the length of each word.
