0
0
Goprogramming~20 mins

Error interface in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Error Interface Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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"}))
}
A
something went wrong
true
B
something went wrong
false
Ccompile error: cannot use MyError literal as error
D
something went wrong
panic: runtime error
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.
Predict Output
intermediate
2: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")
	}
}
Aruntime panic
Bcompile error: cannot assign nil to error
Cerr is nil
Derr is not nil
Attempts:
2 left
💡 Hint
Think about interface values and their underlying types.
🔧 Debug
advanced
2: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))
}
A
wrapped error: base error
false
B
wrapped error: base error
true
Cpanic: runtime error: invalid memory address or nil pointer dereference
Dcompile error: cannot use MyError literal as error
Attempts:
2 left
💡 Hint
Check if MyError implements Unwrap() method for errors.Is to work.
📝 Syntax
advanced
2: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())
}
AChange MyError to interface type
BChange Error() method receiver to pointer: func (e *MyError) Error() string
CNo fix needed; code compiles and runs correctly
DAdd parentheses to Error call: fmt.Println(err.Error)
Attempts:
2 left
💡 Hint
Check if the method signature and usage are correct.
🧠 Conceptual
expert
2: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)
}
A2
B3
C1
D0
Attempts:
2 left
💡 Hint
Count the base error and each wrapping error implementing Unwrap.