Recall & Review
beginner
What is the common way to handle errors in Go?
In Go, errors are handled by returning an error value from functions. You check if the error is not nil to see if something went wrong.
Click to reveal answer
beginner
What does the
error type represent in Go?The
error type is an interface in Go that represents an error condition with a single method Error() that returns a string describing the error.Click to reveal answer
beginner
How do you check if a function returned an error in Go?
You check if the returned error variable is not
nil. If it is not nil, an error occurred and you can handle it accordingly.Click to reveal answer
intermediate
What is the purpose of the
defer statement in error handling?defer schedules a function call to run after the current function finishes. It is often used to clean up resources even if an error occurs.Click to reveal answer
intermediate
How can you create a custom error in Go?
You can create a custom error by using the
errors.New() function or by defining a type that implements the error interface with an Error() method.Click to reveal answer
In Go, what does a function usually return to indicate an error?
✗ Incorrect
Functions return an error value of type error to indicate if something went wrong.
What should you check after calling a function that returns an error?
✗ Incorrect
You check if the error is nil. If it is not nil, an error occurred.
Which Go statement helps ensure cleanup code runs even if an error occurs?
✗ Incorrect
defer schedules cleanup code to run after the function finishes.How do you create a simple custom error message in Go?
✗ Incorrect
Use errors.New("message") to create a new error with a custom message.
What does the
Error() method return in a custom error type?✗ Incorrect
The
Error() method returns a string describing the error.Explain how error handling works in Go and why it is important to check errors after function calls.
Think about how Go functions return errors and what happens if you ignore them.
You got /4 concepts.
Describe how you can create and use a custom error type in Go.
Consider both simple and advanced ways to make your own error messages.
You got /4 concepts.