A custom error type in Go is a user-defined type that implements the error interface by having an Error() string method. It allows you to create errors with additional context or behavior beyond the basic error message.
Click to reveal answer
beginner
How do you define a custom error type in Go?
You define a custom error type by creating a struct or type and implementing the Error() string method on it. For example:
type MyError struct { Msg string }
func (e MyError) Error() string { return e.Msg }
Click to reveal answer
intermediate
Why use custom error types instead of plain errors?
Custom error types let you add extra information or methods to errors. This helps in identifying error causes, handling errors differently, or adding context like error codes or timestamps.
Click to reveal answer
intermediate
How can you check for a specific custom error type in Go?
You can use a type assertion or errors.As to check if an error is a specific custom error type. For example:
var myErr MyError
if errors.As(err, &myErr) { /* handle MyError */ }
Click to reveal answer
beginner
Show a simple example of a custom error type with additional context.
Example:
type NotFoundError struct { Resource string }
func (e NotFoundError) Error() string {
return fmt.Sprintf("%s not found", e.Resource)
}
This error tells which resource was not found.
Click to reveal answer
What method must a custom error type implement in Go?
AError() string
BString() string
CMessage() string
DToString() string
✗ Incorrect
The error interface in Go requires the Error() string method.
Which Go feature helps you check if an error is a specific custom error type?
APanic recovery
BType assertion or errors.As
CReflection only
DInterface embedding
✗ Incorrect
Type assertion or errors.As lets you check and extract specific error types.
Why might you create a custom error type instead of using errors.New?
ATo make errors print in uppercase
BTo make errors faster
CTo avoid using the error interface
DTo add extra information or behavior to the error
✗ Incorrect
Custom error types allow adding context or methods to errors.
What is the return type of the Error() method in a custom error type?
Astring
Bint
Cbool
Derror
✗ Incorrect
The Error() method returns a string describing the error.
Which of these is a valid custom error type declaration in Go?
Avar MyError = errors.New("error")
Bfunc MyError() error { return "error" }
Ctype MyError struct { Msg string }
Dtype MyError int
✗ Incorrect
A struct with fields is a common way to define a custom error type.
Explain how to create and use a custom error type in Go.
Think about how Go's error interface works and how you can add extra info.
You got /4 concepts.
Why are custom error types useful in Go programming?
Consider real-life situations where knowing more about an error helps.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of creating a custom error type in Go?
easy
A. To add extra information and context to errors
B. To make errors print in uppercase letters
C. To automatically fix errors in the code
D. To prevent errors from being returned
Solution
Step 1: Understand the role of custom error types
Custom error types allow programmers to include more details about what caused the error, beyond just a simple message.
Step 2: Compare options with this understanding
Only To add extra information and context to errors correctly describes adding extra information and context. Other options describe unrelated or incorrect behaviors.
Final Answer:
To add extra information and context to errors -> Option A
Quick Check:
Custom error types add context = B [OK]
Hint: Custom errors add details, not fix or hide errors [OK]
2. Which of the following is the correct way to define a custom error type in Go?
easy
A. type MyError string func (e MyError) Print() { fmt.Println(e) }
B. func MyError() string { return "error" }
C. type MyError struct { msg string } func (e MyError) Error() string { return e.msg }
D. var MyError = errors.New("error")
Solution
Step 1: Recall how to create custom error types
In Go, a custom error type is a struct that implements the Error() string method.
Step 2: Check each option
type MyError struct { msg string } func (e MyError) Error() string { return e.msg } defines a struct and implements Error() correctly. Options A, B, and D do not implement the Error() method properly or define a struct.
Final Answer:
type MyError struct { msg string } func (e MyError) Error() string { return e.msg } -> Option C
Defining error as a function without Error() method
Using string type without Error() method
Using errors.New instead of custom struct
3. What will be the output of this Go code?
package main
import (
"errors"
"fmt"
)
type MyError struct {
Code int
}
func (e MyError) Error() string {
return fmt.Sprintf("Error code: %d", e.Code)
}
func main() {
var err error = MyError{Code: 404}
if errors.As(err, &MyError{}) {
fmt.Println("Custom error detected")
} else {
fmt.Println("Unknown error")
}
}
medium
A. Unknown error
B. Custom error detected
C. Compilation error
D. Runtime panic
Solution
Step 1: Understand errors.As usage
errors.As(err, targetPtr) returns true if err matches the type pointed to by targetPtr.
Step 2: Analyze the code's errors.As call
&MyError{} is a pointer to type MyError. err is MyError{Code: 404}, which matches MyError exactly. errors.As succeeds and prints "Custom error detected".
Final Answer:
Custom error detected -> Option B
Quick Check:
errors.As(MyError, &MyError{}) matches [OK]
Hint: errors.As works with &MyError{} (pointer to literal) [OK]
Common Mistakes:
Passing &MyError{} instead of pointer to variable
Assuming errors.As works with value types
Confusing errors.Is with errors.As
4. Identify the error in this Go code snippet that tries to use a custom error type:
A. MyError should not have a pointer receiver for Error()
B. Error() method must return an int, not string
C. MyError struct must embed error interface
D. doSomething returns MyError value but Error() has pointer receiver
Solution
Step 1: Check method receiver and returned type
Error() is defined with pointer receiver (*MyError), but doSomething returns a value (MyError), not a pointer.
Step 2: Understand interface satisfaction rules
Because Error() has pointer receiver, only *MyError implements error interface, not MyError value. Returning MyError value causes a type mismatch.
Final Answer:
doSomething returns MyError value but Error() has pointer receiver -> Option D
Quick Check:
Pointer receiver means only pointer type implements error [OK]
Hint: Pointer receiver means return pointer, not value [OK]
Common Mistakes:
Returning value when method has pointer receiver
Thinking struct must embed error interface
Expecting Error() to return int
5. You want to create a custom error type that holds an error code and message, and you want to check for this error type using errors.As. Which of the following code snippets correctly implements this pattern?
hard
A. type CodeError struct { Code int; Msg string } func (e *CodeError) Error() string { return e.Msg } func main() { var err error = &CodeError{Code: 123, Msg: "fail"} var target *CodeError if errors.As(err, &target) { fmt.Println(target.Code) } }
B. type CodeError struct { Code int; Msg string } func (e CodeError) Error() string { return e.Msg } func main() { var err error = CodeError{Code: 123, Msg: "fail"} var target *CodeError if errors.As(err, &target) { fmt.Println(target.Code) } }
C. type CodeError struct { Code int; Msg string } func (e CodeError) Error() string { return e.Msg } func main() { var err error = &CodeError{Code: 123, Msg: "fail"} var target CodeError if errors.As(err, &target) { fmt.Println(target.Code) } }
D. type CodeError struct { Code int; Msg string } func (e *CodeError) Error() string { return e.Msg } func main() { var err error = CodeError{Code: 123, Msg: "fail"} var target *CodeError if errors.As(err, &target) { fmt.Println(target.Code) } }
Solution
Step 1: Check receiver type and error assignment
type CodeError struct { Code int; Msg string } func (e *CodeError) Error() string { return e.Msg } func main() { var err error = &CodeError{Code: 123, Msg: "fail"} var target *CodeError if errors.As(err, &target) { fmt.Println(target.Code) } } uses pointer receiver for Error() and assigns &CodeError{} to err, which matches the pointer receiver.
Step 2: Check errors.As usage
type CodeError struct { Code int; Msg string } func (e *CodeError) Error() string { return e.Msg } func main() { var err error = &CodeError{Code: 123, Msg: "fail"} var target *CodeError if errors.As(err, &target) { fmt.Println(target.Code) } } passes &target where target is *CodeError, which is correct usage for errors.As to detect the custom error type.
Final Answer:
custom error with pointer receiver and errors.As check -> Option A