Bird
Raised Fist0
Goprogramming~20 mins

Error interface in Go - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1.

What does the Error() method in Go's error interface return?

easy
A. A boolean indicating success or failure
B. An integer error code
C. A string describing the error message
D. A pointer to the error object

Solution

  1. Step 1: Understand the error interface

    The error interface in Go defines a single method Error() that returns a string.
  2. Step 2: Identify the return type of Error()

    The Error() method returns a string that describes the error message.
  3. Final Answer:

    A string describing the error message -> Option C
  4. Quick Check:

    Error() returns string message [OK]
Hint: Error() always returns a string message describing the problem [OK]
Common Mistakes:
  • Thinking Error() returns an integer code
  • Assuming Error() returns a boolean
  • Confusing Error() with returning an error object pointer
2.

Which of the following is the correct way to declare a function in Go that returns an error?

func doSomething() ???
easy
A. func doSomething() error
B. func doSomething() string
C. func doSomething() (int, error)
D. func doSomething() *error

Solution

  1. Step 1: Recall function return types for errors

    In Go, functions that can fail often return an error type as the last return value.
  2. Step 2: Check the syntax for returning only an error

    The correct syntax for a function returning only an error is func doSomething() error.
  3. Final Answer:

    func doSomething() error -> Option A
  4. Quick Check:

    Function returns error type only [OK]
Hint: Use 'error' as return type to indicate possible failure [OK]
Common Mistakes:
  • Returning *error instead of error
  • Returning string instead of error
  • Returning multiple values without parentheses
3.

What will be the output of the following Go program?

package main
import (
	"errors"
	"fmt"
)
func main() {
	err := errors.New("file not found")
	if err != nil {
		fmt.Println(err.Error())
	} else {
		fmt.Println("No error")
	}
}
medium
A. file not found
B. No error
C. nil
D. Compilation error

Solution

  1. Step 1: Understand errors.New usage

    The errors.New function creates a new error with the message "file not found".
  2. Step 2: Check the if condition and output

    Since err is not nil, the program prints err.Error(), which is the string "file not found".
  3. Final Answer:

    file not found -> Option A
  4. Quick Check:

    Non-nil error prints message [OK]
Hint: errors.New creates error with message; err != nil prints message [OK]
Common Mistakes:
  • Expecting 'No error' output
  • Thinking err is nil after errors.New
  • Confusing err.Error() with err itself
4.

Identify the error in this Go code snippet:

func check() error {
	var err error
	if err != nil {
		return err
	}
	return nil
}
medium
A. Missing parentheses in if condition
B. Variable err is never assigned, so err != nil is always false
C. Function must return a string, not error
D. Cannot return nil from a function returning error

Solution

  1. Step 1: Analyze variable initialization

    The variable err is declared but never assigned a value, so it defaults to nil.
  2. Step 2: Evaluate the if condition

    Since err is nil, the condition err != nil is false, so the return inside if never runs.
  3. Final Answer:

    Variable err is never assigned, so err != nil is always false -> Option B
  4. Quick Check:

    Unassigned error variable is nil [OK]
Hint: Uninitialized error is nil, so err != nil is false [OK]
Common Mistakes:
  • Assuming err is non-nil by default
  • Thinking function must return string
  • Confusing nil return with error
5.

Given this Go function that returns an error, how can you modify it to wrap the original error with additional context?

func readFile() error {
	return errors.New("file missing")
}

Choose the best way to add context "readFile failed:" before the original error message.

hard
A. return fmt.Errorf("readFile failed: %v", errors.New("file missing"))
B. return errors.New("readFile failed: file missing")
C. return errors.Wrap(errors.New("file missing"), "readFile failed")
D. return fmt.Errorf("readFile failed: %w", errors.New("file missing"))

Solution

  1. Step 1: Understand error wrapping in Go

    Go 1.13+ supports error wrapping using fmt.Errorf with the %w verb to wrap an error with context.
  2. Step 2: Check options for wrapping

    return fmt.Errorf("readFile failed: %w", errors.New("file missing")) uses fmt.Errorf("readFile failed: %w", errors.New("file missing")), which correctly wraps the original error with context.
  3. Step 3: Identify incorrect options

    return errors.New("readFile failed: file missing") creates a new error string but does not wrap the original error. return errors.Wrap(errors.New("file missing"), "readFile failed") uses errors.Wrap which is not in standard Go. return fmt.Errorf("readFile failed: %v", errors.New("file missing")) uses %v which formats but does not wrap the error.
  4. Final Answer:

    return fmt.Errorf("readFile failed: %w", errors.New("file missing")) -> Option D
  5. Quick Check:

    Use fmt.Errorf with %w to wrap errors [OK]
Hint: Use fmt.Errorf with %w to wrap errors and add context [OK]
Common Mistakes:
  • Using %v instead of %w for wrapping
  • Creating new error string without wrapping
  • Using non-standard errors.Wrap function
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard