0
0
Goprogramming~5 mins

Multiple return values in Go - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does it mean when a Go function has multiple return values?
It means the function can send back more than one result at the same time, like returning both a value and an error.
Click to reveal answer
beginner
How do you declare a function in Go that returns two values, an int and an error?
You write the return types in parentheses, like: func example() (int, error).
Click to reveal answer
beginner
Why are multiple return values useful in Go?
They let you return extra information, like an error, without needing special structures. This helps keep code simple and clear.
Click to reveal answer
intermediate
Given this function signature: func divide(a, b int) (int, error), what should the function return if b is zero?
It should return zero and an error explaining division by zero is not allowed.
Click to reveal answer
beginner
How do you capture multiple return values from a function call in Go?
You assign them to multiple variables separated by commas, like: result, err := divide(10, 2).
Click to reveal answer
What is the correct way to declare a Go function that returns a string and an error?
Afunc example() string error
Bfunc example() (string, error)
Cfunc example() string, error
Dfunc example() (string error)
How do you receive two return values from a Go function call?
Aresult := function()
Bresult; err := function()
Cresult, err := function()
Dresult err = function()
Why might a Go function return an error as a second value?
ATo confuse the programmer
BTo return a second result always
CTo slow down the program
DTo indicate if something went wrong during execution
What happens if you ignore the second return value (error) in Go?
AYou can ignore it but risk missing errors
BThe program will not compile
CThe function returns only one value
DThe error is automatically handled
Which of these is a valid way to return multiple values in Go?
Areturn value1, value2
Breturn (value1, value2)
Creturn [value1, value2]
Dreturn {value1, value2}
Explain how multiple return values work in Go and why they are useful.
Think about how functions can send back both results and errors.
You got /3 concepts.
    Describe how you would write and call a Go function that returns a number and an error.
    Remember to use parentheses for return types and comma to receive values.
    You got /3 concepts.