0
0
Goprogramming~10 mins

Why concurrency is needed in Go - Test Your Understanding

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.

Go
go [1]()
Drag options to blanks, or click blank then click option'
Afmt.Println
Bmain
CsayHello
Dtime.Sleep
Attempts:
3 left
💡 Hint
Common Mistakes
Using fmt.Println directly after go without calling a function.
Trying to start the main function as a goroutine.
Using time.Sleep without parentheses.
2fill in blank
medium

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

Go
done := make(chan bool)
go func() {
    fmt.Println("Working...")
    done [1] true
}()
<-done
Drag options to blanks, or click blank then click option'
A<-
B<-chan
D<-chan bool
Attempts:
3 left
💡 Hint
Common Mistakes
Using the receive operator <- on the left side incorrectly.
Confusing channel type declarations with send/receive operations.
3fill in blank
hard

Fix the error in the code to correctly launch multiple goroutines.

Go
for i := 0; i < 3; i++ {
    go func() {
        [1]
        fmt.Println(i)
    }()
}
Drag options to blanks, or click blank then click option'
Avar i
Bi
Ci int
Di := i
Attempts:
3 left
💡 Hint
Common Mistakes
Not capturing the loop variable causes all goroutines to print the same value.
Trying to declare i with a type inside the loop incorrectly.
4fill in blank
hard

Fill both blanks to create a buffered channel and send a value.

Go
ch := make(chan int, [1])
ch [2] 5
Drag options to blanks, or click blank then click option'
A1
B<-
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using zero as buffer size creates an unbuffered channel.
Using the receive operator instead of send operator.
5fill in blank
hard

Fill all three blanks to create a map with word lengths using concurrency.

Go
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])
}
Drag options to blanks, or click blank then click option'
Aw
Cword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the loop variable word directly inside the goroutine causes race conditions.
Not locking the map before writing causes concurrent map writes error.