0
0
Goprogramming~10 mins

Goroutine creation - 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 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'
Astart
Bfunc
Cdefer
Dgo
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.
2fill in blank
medium

Complete 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'
Ago
Bdefer
Cfunc
Dstart
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.
3fill in blank
hard

Fix 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'
Afunc
Bgo
Cdefer
Dstart
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.
4fill in blank
hard

Fill 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'
Ago
Bi
Cn
Ddefer
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.
5fill in blank
hard

Fill 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'
Anum
Bgo
C*
Di
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'i' instead of 'num' as the variable name.
Omitting the 'go' keyword.
Using '+' instead of '*' for multiplication.