Recall & Review
beginner
What is the common way to return errors in Go functions?
In Go, functions often return an error as the last return value. If there is no error, the error value is nil.
Click to reveal answer
beginner
How do you check if a function returned an error in Go?
You compare the returned error value to nil. If it is not nil, an error occurred and should be handled.
Click to reveal answer
intermediate
What does the following Go code return?
func divide(a, b int) (int, error) {
if b == 0 {
return 0, fmt.Errorf("cannot divide by zero")
}
return a / b, nil
}This function returns the result of a divided by b and nil error if b is not zero. If b is zero, it returns 0 and an error with the message "cannot divide by zero".
Click to reveal answer
intermediate
Why is it useful to return errors instead of panicking in Go?
Returning errors allows the caller to decide how to handle the problem, making the program more robust and easier to maintain.
Click to reveal answer
beginner
What package is commonly used to create error values in Go?
The fmt package is commonly used with fmt.Errorf to create formatted error messages.
Click to reveal answer
In Go, what does a function usually return to indicate an error?
✗ Incorrect
Go functions typically return an error type as the last return value to indicate if an error occurred.
How do you check if a Go function returned an error?
✗ Incorrect
In Go, an error is nil if no error occurred. If it is not nil, an error happened.
What does fmt.Errorf do in Go?
✗ Incorrect
fmt.Errorf creates an error value with a formatted message.
What should you do if a Go function returns a non-nil error?
✗ Incorrect
You should handle errors to keep your program stable and predictable.
Which of these is a correct function signature for returning an error in Go?
✗ Incorrect
The correct syntax places error as the last return type: (int, error).
Explain how to return and check errors in a Go function.
Think about how Go functions signal problems and how callers respond.
You got /3 concepts.
Describe why returning errors is preferred over panicking in Go programs.
Consider the difference between stopping a program and letting it recover.
You got /3 concepts.