Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a string instead of an integer as the first value.
Returning only one value instead of two.
✗ Incorrect
The function returns an integer and a string. The first return value should be an integer like 5.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Printing variable a twice.
Using a string "b" instead of variable b.
✗ Incorrect
The second returned value is stored in variable b, so we print b.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Writing multiple types without parentheses.
Using invalid syntax like 'int int'.
✗ Incorrect
In Go, to return multiple values, you use parentheses around the types: (int, int).
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same variable name twice.
Using variables not matching the print statement.
✗ Incorrect
The variables a and b receive the two return values from getValues().
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning values in wrong order.
Using false instead of true.
✗ Incorrect
The function returns an int 7, a string "seven", and a bool true.