Discover key principles of error handling and the panic/recover mechanism in Go. This quiz helps reinforce understanding of error values, panic usage, recover patterns, and idiomatic approaches in Go programming.
Which interface in Go is used to represent an error value returned from functions?
Explanation: The 'error' interface is built into Go for error handling and is commonly used as a return type for functions, allowing errors to be checked. 'Exception' and 'fail' do not exist in the Go standard library, and 'Err' is not an interface but may be used as a variable name. Only 'error' is correct for this purpose.
In Go, what is the standard way to handle a function that returns both a result and an error?
Explanation: Go code typically checks if the error returned from a function is nil before using the result, ensuring errors are caught early. Go does not support try-catch blocks like some other languages. Ignoring errors or always throwing them is not recommended and goes against idiomatic Go practices.
Which method enables you to define a custom error type in Go?
Explanation: To create a custom error type in Go, you implement the Error() string method for your type, satisfying the error interface. 'Exception struct' and 'MakeErrorFunction' are not recognized patterns in Go, and there is no 'error.NewType' method available.
What does the panic function do in a Go program?
Explanation: Calling panic causes the program to halt normal execution and begins to unwind the stack, triggering deferred functions. Panic does not just return an error, log a warning, or restart the application—those options describe unrelated or incorrect behaviors.
Where should the recover function typically be called in order to handle a panic?
Explanation: Recover must be called inside a deferred function to intercept a panic and regain control. If used anywhere else, recover will not catch the panic. Calling it in main, in a for loop, or at the global level is incorrect and will not work as intended.
What is the zero value of the error type in Go?
Explanation: Before an error is assigned, its zero value is nil, which Go uses to indicate no error is present. An empty string, false, or zero are not valid zero values for the error type, as error is an interface, not a primitive data type.
Which is the most suitable scenario to use panic instead of returning an error?
Explanation: Panic is intended for unrecoverable errors that indicate a programming bug or a state that cannot be gracefully handled. Input validation, missing files, and parsing errors should return error values, as they are expected and can be managed more gracefully.
Which standard Go pattern enables functions to return both useful results and error information?
Explanation: Functions often return a tuple of (value, error), allowing callers to check for errors before using the result. Global error variables are prone to conflicts, returning only error codes lacks context, and Go does not support exception throwing in the standard sense.
Which function would you use to create a formatted error value that includes context information?
Explanation: fmt.Errorf is used to create formatted error messages, providing valuable context with variables. log.Fatal terminates the program and is not for error creation, panicf is not a standard function, and error.NewFormatted does not exist in Go's libraries.
What will happen if recover is called outside of a deferred function during a panic in Go?
Explanation: Recover only works inside a deferred function during panic unwinding. If called outside of this context, it returns nil and has no effect. It will not cancel the panic, restart main, or automatically log the panic anywhere.