Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start a new goroutine that calls the function sayHello.
Go
func sayHello() {
println("Hello from goroutine")
}
func main() {
[1] sayHello()
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'func' instead of 'go' to start a goroutine.
Using 'defer' which delays execution until the surrounding function returns.
Using 'start' which is not a Go keyword.
✗ Incorrect
The keyword 'go' is used to start a new goroutine in Go.
2fill in blank
mediumComplete the code to start a goroutine that prints "Hi" using an anonymous function.
Go
func main() {
[1] func() {
println("Hi")
}()
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the 'go' keyword and just calling the function normally.
Using 'defer' which delays execution.
Using 'start' which is not valid in Go.
✗ Incorrect
The 'go' keyword starts the anonymous function as a goroutine.
3fill in blank
hardFix the error in the code to correctly start a goroutine that calls printNumber with argument 5.
Go
func printNumber(n int) {
println(n)
}
func main() {
[1] printNumber(5)
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to add 'go' before the function call.
Using 'func' which declares a function but does not start a goroutine.
Using 'defer' which delays execution.
✗ Incorrect
The 'go' keyword is required to start the function call as a goroutine.
4fill in blank
hardFill both blanks to create a goroutine that prints numbers from 1 to 3 using a loop.
Go
func main() {
for i := 1; i <= 3; i++ {
[1] func(n int) {
println(n)
}([2])
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'defer' instead of 'go' to start the goroutine.
Passing the wrong variable name to the function.
Not passing any argument to the function.
✗ Incorrect
Use 'go' to start the goroutine and pass 'i' as the argument to the function.
5fill in blank
hardFill all three blanks to create a goroutine that prints the square of each number in the slice.
Go
func main() {
nums := []int{2, 4, 6}
for _, [1] := range nums {
[2] func(n int) {
println(n [3] n)
}([1])
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'i' instead of 'num' as the variable name.
Omitting the 'go' keyword.
Using '+' instead of '*' for multiplication.
✗ Incorrect
Use 'num' as the variable name, 'go' to start the goroutine, and '*' to multiply the number by itself.