Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start a new goroutine.
Go
go [1]() Drag options to blanks, or click blank then click option'
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.✗ Incorrect
The go keyword starts a new goroutine running the function sayHello.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the receive operator
<- on the left side incorrectly.Confusing channel type declarations with send/receive operations.
✗ Incorrect
To send a value to a channel, use done <- true.
3fill in blank
hardFix 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'
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.✗ Incorrect
Passing i as a parameter inside the goroutine fixes the closure capturing issue.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using zero as buffer size creates an unbuffered channel.
Using the receive operator instead of send operator.
✗ Incorrect
A buffered channel of size 1 is created, then the value 5 is sent using ch <- 5.
5fill in blank
hardFill 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'
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.
✗ Incorrect
The goroutine uses the parameter w to safely update the map with the length of each word.