Complete the code to start a new goroutine that prints "Hello".
go [1]()The go keyword starts a new goroutine. You need to call a function, here PrintHello, to run concurrently.
Complete the code to wait for a goroutine to finish using a channel.
done := make(chan bool)
go func() {
// do work
done[1]true
}()
<-doneTo send a value into a channel, use the <- operator pointing to the channel: done <- true.
Fix the error in the code that launches a goroutine with a loop variable.
for i := 0; i < 3; i++ { go func([1] int) { fmt.Println([1]) }(i) }
The loop variable i must be passed as a parameter to the goroutine function to capture its current value. Without it, all goroutines print the last value.
Fill both blanks to create a goroutine that signals completion using a WaitGroup.
var wg sync.WaitGroup wg.[1](1) go func() { defer wg.[2]() // work here }() wg.Wait()
wg.Add(1) tells the WaitGroup to wait for one goroutine. Inside the goroutine, defer wg.Done() signals when it finishes. Finally, wg.Wait() blocks until done.
Fill both blanks to create a buffered channel and send/receive data correctly.
ch := make(chan int, [1]) go func() { ch[2]42 }() value := <-ch
The channel is created with buffer size 1. Sending uses ch <- 42. Receiving uses <- ch, but here the blank after ch is empty because the receive operator is before the channel.