0
0
Goprogramming~20 mins

Custom error types in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Go Custom Error Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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())
}
AError 404: Not Found
BMyError{Code:404, Message:"Not Found"}
C404 Not Found
Druntime error: invalid memory address or nil pointer dereference
Attempts:
2 left
💡 Hint
Look at how the Error() method formats the string.
Predict Output
intermediate
2: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)
}
Apanic: runtime error: invalid memory address or nil pointer dereference
B&{something went wrong}
C<nil>
Dsomething went wrong
Attempts:
2 left
💡 Hint
Printing an error interface calls its Error() method.
🔧 Debug
advanced
2: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())
}
ACannot assign MyError to error interface without pointer receiver
BError() method must return a string, but here it returns nothing
CMyError struct must embed error interface explicitly
Dfmt.Println cannot print error interface values
Attempts:
2 left
💡 Hint
Check the signature of the Error() method in the error interface.
🧠 Conceptual
advanced
2: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?
ATo avoid copying the error struct and allow modifying fields if needed
BBecause only pointer receivers satisfy the error interface
CTo prevent the error from being nil
DBecause Go requires all methods on structs to use pointer receivers
Attempts:
2 left
💡 Hint
Think about efficiency and mutability when using pointer receivers.
Predict Output
expert
2: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")
	}
}
Apanic: interface conversion: error is *errors.errorString, not MyError
BMatched MyError
CDid not match MyError
Dcode 0
Attempts:
2 left
💡 Hint
errors.As tries to find the target type in the error chain.