0
0
Goprogramming~10 mins

Goroutine lifecycle - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start a new goroutine that prints "Hello".

Go
go [1]()
Drag options to blanks, or click blank then click option'
APrintHello
Bfmt.Println
Chello
DprintHello
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting parentheses after the function name
Using a variable name instead of a function
Not using the go keyword
2fill in blank
medium

Complete the code to wait for a goroutine to finish using a channel.

Go
done := make(chan bool)
go func() {
    // do work
    done[1]true
}()
<-done
Drag options to blanks, or click blank then click option'
A->
B<-
C=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using = instead of <- to send to channel
Using -> which is invalid syntax
Using == which is a comparison operator
3fill in blank
hard

Fix the error in the code that launches a goroutine with a loop variable.

Go
for i := 0; i < 3; i++ {
    go func([1] int) {
        fmt.Println([1])
    }(i)
}
Drag options to blanks, or click blank then click option'
Aindex
B0
Cj
Di
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name not defined
Not passing the loop variable to the goroutine
Expecting goroutines to capture loop variable automatically
4fill in blank
hard

Fill both blanks to create a goroutine that signals completion using a WaitGroup.

Go
var wg sync.WaitGroup
wg.[1](1)
go func() {
    defer wg.[2]()
    // work here
}()
wg.Wait()
Drag options to blanks, or click blank then click option'
AAdd
BDone
CWait
DSignal
Attempts:
3 left
💡 Hint
Common Mistakes
Using Wait instead of Add to set count
Forgetting to call Done inside goroutine
Calling Signal which is not a WaitGroup method
5fill in blank
hard

Fill both blanks to create a buffered channel and send/receive data correctly.

Go
ch := make(chan int, [1])
go func() {
    ch[2]42
}()
value := <-ch
Drag options to blanks, or click blank then click option'
A1
B<-
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 for buffer size which makes it unbuffered
Placing receive operator after channel name
Leaving blanks with wrong operators