0
0
Goprogramming~10 mins

Multiple return values in Go - Interactive Code Practice

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

Complete the code to return two values from the function.

Go
func getValues() (int, string) {
    return [1], "hello"
}
Drag options to blanks, or click blank then click option'
A5
B"five"
Chello
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a string instead of an integer as the first value.
Returning only one value instead of two.
2fill in blank
medium

Complete the code to assign multiple return values to variables.

Go
func main() {
    a, b := getValues()
    fmt.Println(a, [1])
}
Drag options to blanks, or click blank then click option'
Ab
Bc
C"b"
Da
Attempts:
3 left
💡 Hint
Common Mistakes
Printing variable a twice.
Using a string "b" instead of variable b.
3fill in blank
hard

Fix the error in the function signature to return two values.

Go
func calculate() [1] {
    return 10, 20
}
Drag options to blanks, or click blank then click option'
Aint
Bint int
Cint, int
D(int, int)
Attempts:
3 left
💡 Hint
Common Mistakes
Writing multiple types without parentheses.
Using invalid syntax like 'int int'.
4fill in blank
hard

Fill both blanks to declare and assign multiple return values correctly.

Go
func main() {
    [1], [2] := getValues()
    fmt.Println(a, b)
}
Drag options to blanks, or click blank then click option'
Aa
Bb
Cc
Dd
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same variable name twice.
Using variables not matching the print statement.
5fill in blank
hard

Fill all three blanks to create a function that returns three values and assign them.

Go
func getThree() (int, string, bool) {
    return [1], [2], [3]
}

func main() {
    x, y, z := getThree()
    fmt.Println(x, y, z)
}
Drag options to blanks, or click blank then click option'
A7
B"seven"
Ctrue
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Returning values in wrong order.
Using false instead of true.