In Go, errors are represented as a built-in interface type, 'error', which is a conventional interface for handling error conditions.
2. How do you create a simple error in Go?
a) errors.New("error message")
b) newError("error message")
c) makeError("error message")
d) error("error message")
Answer:
a) errors.New("error message")
Explanation:
The 'errors' package in Go provides a function 'New' to create basic errors with a given error message.
3. How is error handling typically done in Go functions?
a) Using try-catch blocks
b) Returning an error as the last return value
c) Ignoring errors
d) Using global error variables
Answer:
b) Returning an error as the last return value
Explanation:
In Go, functions typically return an error as their last return value. The caller should check this value to handle the error.
4. What does a nil error signify in Go?
a) A pending error
b) A critical error
c) No error occurred
d) An unidentified error
Answer:
c) No error occurred
Explanation:
In Go, a nil error value indicates that no error has occurred.
5. How do you check for an error in Go?
a) Using the check() function
b) Comparing the error to a known error
c) If the error is not nil
d) Using error handling libraries
Answer:
c) If the error is not nil
Explanation:
In Go, the idiomatic way to check for an error is to compare if the error is not nil.
6. What is the purpose of the 'defer' statement in error handling in Go?
a) To defer the handling of an error
b) To ensure a function is called at the end of a function's execution
c) To delay the return of an error
d) To create custom error handling
Answer:
b) To ensure a function is called at the end of a function's execution
Explanation:
In Go, the 'defer' statement is often used to ensure that a function call is performed later in a program's execution, typically for purposes of cleanup.
7. How do you customize error types in Go?
a) By creating a struct that implements the error interface
b) Using the customError package
c) Defining a new error class
d) By importing specialized error libraries
Answer:
a) By creating a struct that implements the error interface
Explanation:
In Go, custom error types can be created by defining a struct type that implements the Error() method of the error interface.
8. What is the result of calling the Error() method on an error in Go?
a) The error code
b) The error message as a string
c) A boolean indicating if there's an error
d) The type of error
Answer:
b) The error message as a string
Explanation:
The Error() method on an error in Go returns the error message associated with that error as a string.
9. Can multiple errors be returned from a Go function?
a) Yes, using multiple return values
b) No, only one error can be returned
c) Yes, by concatenating errors
d) Yes, using error arrays
Answer:
a) Yes, using multiple return values
Explanation:
While it is unusual, a Go function can return multiple errors by specifying more than one error return value.
10. What is panic in Go?
a) A way to handle errors
b) An error that cannot be recovered
c) A built-in function to stop the ordinary flow of control
d) A syntax error
Answer:
c) A built-in function to stop the ordinary flow of control
Explanation:
The 'panic' function in Go is used to stop the ordinary flow of control and begin panicking, which typically results in the program terminating, unless recovered.