Challenge - 5 Problems
Go Custom Error Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a custom error type's Error() method
What is the output when running this Go program?
Go
package main import ( "errors" "fmt" ) type MyError struct { Code int Message string } func (e MyError) Error() string { return fmt.Sprintf("Error %d: %s", e.Code, e.Message) } func main() { err := MyError{Code: 404, Message: "Not Found"} fmt.Println(err.Error()) }
Attempts:
2 left
💡 Hint
Look at how the Error() method formats the string.
✗ Incorrect
The Error() method returns a formatted string with the code and message. Printing err.Error() outputs exactly that string.
❓ Predict Output
intermediate2:00remaining
Output when returning a custom error type as error interface
What will this program print?
Go
package main import ( "fmt" ) type MyError struct { Msg string } func (e *MyError) Error() string { return e.Msg } func doSomething() error { return &MyError{Msg: "something went wrong"} } func main() { err := doSomething() fmt.Println(err) }
Attempts:
2 left
💡 Hint
Printing an error interface calls its Error() method.
✗ Incorrect
The doSomething function returns a pointer to MyError which implements Error(). Printing err calls Error() and prints the message.
🔧 Debug
advanced2:00remaining
Identify the error in custom error type implementation
This code is supposed to implement a custom error type but causes a compile error. What is the cause?
Go
package main import "fmt" type MyError struct { Msg string } func (e MyError) Error() { fmt.Println(e.Msg) } func main() { var err error = MyError{Msg: "fail"} fmt.Println(err.Error()) }
Attempts:
2 left
💡 Hint
Check the signature of the Error() method in the error interface.
✗ Incorrect
The error interface requires Error() to return a string. Here Error() returns nothing, causing a compile error.
🧠 Conceptual
advanced2:00remaining
Why use pointer receiver for custom error types?
Why is it common to define the Error() method with a pointer receiver for custom error types in Go?
Attempts:
2 left
💡 Hint
Think about efficiency and mutability when using pointer receivers.
✗ Incorrect
Pointer receivers avoid copying the struct and allow methods to modify the receiver if needed. Value receivers copy the struct.
❓ Predict Output
expert2:00remaining
Output of type assertion with custom error type
What is the output of this program?
Go
package main import ( "errors" "fmt" ) type MyError struct { Code int } func (e MyError) Error() string { return fmt.Sprintf("code %d", e.Code) } func main() { err := errors.New("standard error") var myErr MyError if errors.As(err, &myErr) { fmt.Println("Matched MyError") } else { fmt.Println("Did not match MyError") } }
Attempts:
2 left
💡 Hint
errors.As tries to find the target type in the error chain.
✗ Incorrect
The error returned by errors.New is not of type MyError, so errors.As returns false and prints "Did not match MyError".