Challenge - 5 Problems
Error Interface Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of custom error type implementing Error interface
What is the output of this Go program?
Go
package main import ( "errors" "fmt" ) type MyError struct { msg string } func (e MyError) Error() string { return e.msg } func main() { var err error = MyError{"something went wrong"} fmt.Println(err.Error()) fmt.Println(errors.Is(err, MyError{"something went wrong"})) }
Attempts:
2 left
💡 Hint
Remember how errors.Is compares errors: it checks err == target directly (no Unwrap), and struct values are equal if fields match.
✗ Incorrect
The custom error type MyError implements the Error() method, so err.Error() prints the message. errors.Is first checks if err == target since MyError does not implement Unwrap(). Struct values compare equal if their fields are equal, so it returns true.
❓ Predict Output
intermediate2:00remaining
Output when error interface is nil but underlying value is not nil
What will this Go program print?
Go
package main import "fmt" type MyError struct{} func (e *MyError) Error() string { return "error occurred" } func returnsNilError() error { var e *MyError = nil return e } func main() { err := returnsNilError() if err == nil { fmt.Println("err is nil") } else { fmt.Println("err is not nil") } }
Attempts:
2 left
💡 Hint
Think about interface values and their underlying types.
✗ Incorrect
The function returns a typed nil (*MyError) as an error interface. The interface value is not nil because it holds a type (*MyError) even though the pointer is nil. So err != nil and prints "err is not nil".
🔧 Debug
advanced2:00remaining
Identify the error in custom error wrapping
This code tries to wrap an error but does not behave as expected. What error does it produce when run?
Go
package main import ( "errors" "fmt" ) type MyError struct { msg string err error } func (e MyError) Error() string { return e.msg + ": " + e.err.Error() } func main() { var baseErr error = errors.New("base error") var err error = MyError{"wrapped error", baseErr} fmt.Println(err) fmt.Println(errors.Is(err, baseErr)) }
Attempts:
2 left
💡 Hint
Check if MyError implements Unwrap() method for errors.Is to work.
✗ Incorrect
MyError implements Error() but does not implement Unwrap(). errors.Is uses Unwrap() to check wrapped errors. Without Unwrap(), errors.Is returns false even if the inner error matches.
📝 Syntax
advanced2:00remaining
Identify the syntax error in this error interface implementation
Which option shows the correct fix for the syntax error in this code snippet?
func (e MyError) Error() string {
return e.msg
}
var err error = MyError{"fail"}
fmt.Println(err.Error())
Go
package main import "fmt" type MyError struct { msg string } func (e MyError) Error() string { return e.msg } func main() { var err error = MyError{"fail"} fmt.Println(err.Error()) }
Attempts:
2 left
💡 Hint
Check if the method signature and usage are correct.
✗ Incorrect
The code is correct as is. The Error() method has a value receiver and is called correctly. No syntax error exists.
🧠 Conceptual
expert2:00remaining
How many items are in the error chain?
Given this code, how many errors are in the chain when using errors.Unwrap repeatedly until nil?
Go
package main import ( "errors" "fmt" ) type MyError struct { msg string err error } func (e *MyError) Error() string { return e.msg } func (e *MyError) Unwrap() error { return e.err } func main() { base := errors.New("base") wrap1 := &MyError{"wrap1", base} wrap2 := &MyError{"wrap2", wrap1} count := 0 for err := error(wrap2); err != nil; err = errors.Unwrap(err) { count++ } fmt.Println(count) }
Attempts:
2 left
💡 Hint
Count the base error and each wrapping error implementing Unwrap.
✗ Incorrect
The chain is wrap2 -> wrap1 -> base. Each unwrap moves one step down. So total 3 errors in the chain.