0
0
Goprogramming~20 mins

Handling errors in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Error Handling Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of error handling with multiple returns
What is the output of this Go program when the function returns an error?
Go
package main

import (
	"errors"
	"fmt"
)

func checkValue(val int) (string, error) {
	if val < 0 {
		return "", errors.New("negative value")
	}
	return "Value is positive", nil
}

func main() {
	msg, err := checkValue(-5)
	if err != nil {
		fmt.Println("Error:", err)
	} else {
		fmt.Println(msg)
	}
}
ACompilation error
BValue is positive
CError: <nil>
DError: negative value
Attempts:
2 left
💡 Hint
Check what the function returns when the input is negative.
Predict Output
intermediate
2:00remaining
Result of deferred function with error handling
What will be printed by this Go program?
Go
package main

import "fmt"

func main() {
	var err error
	defer func() {
		if err != nil {
			fmt.Println("Deferred error:", err)
		} else {
			fmt.Println("No error")
		}
	}()
	err = fmt.Errorf("something went wrong")
	fmt.Println("Main function running")
}
A
Main function running
No error
B
Main function running
Deferred error: something went wrong
C
Deferred error: something went wrong
Main function running
DCompilation error
Attempts:
2 left
💡 Hint
Remember when deferred functions run and the value of err at that time.
🔧 Debug
advanced
2:00remaining
Identify the runtime error in error handling
What runtime error will this Go program produce?
Go
package main

import (
	"errors"
	"fmt"
)

func riskyOperation() error {
	return errors.New("operation failed")
}

func main() {
	var err error
	err = riskyOperation()
	if err != nil {
		panic(err.Error())
	}
	fmt.Println("Operation succeeded")
}
Apanic: operation failed
BOperation succeeded
CCompilation error: missing import
DNo output
Attempts:
2 left
💡 Hint
Check what happens when panic is called with an error message.
🧠 Conceptual
advanced
2:00remaining
Understanding error wrapping and unwrapping
Given the following code, what will be the output?
Go
package main

import (
	"errors"
	"fmt"
)

func main() {
	baseErr := errors.New("base error")
	wrappedErr := fmt.Errorf("wrapped: %w", baseErr)

	if errors.Is(wrappedErr, baseErr) {
		fmt.Println("Error matches base error")
	} else {
		fmt.Println("Error does not match")
	}
}
AError matches base error
BError does not match
CCompilation error: invalid format verb
DNo output
Attempts:
2 left
💡 Hint
Look at how %w is used in fmt.Errorf and how errors.Is works.
Predict Output
expert
3:00remaining
Output of concurrent error handling with channels
What will this Go program print?
Go
package main

import (
	"errors"
	"fmt"
	"sync"
)

func worker(id int, ch chan error, wg *sync.WaitGroup) {
	defer wg.Done()
	if id%2 == 0 {
		ch <- nil
	} else {
		ch <- errors.New(fmt.Sprintf("error from worker %d", id))
	}
}

func main() {
	ch := make(chan error, 5)
	var wg sync.WaitGroup

	for i := 1; i <= 5; i++ {
		wg.Add(1)
		go worker(i, ch, &wg)
	}

	wg.Wait()
	close(ch)

	countErrors := 0
	for err := range ch {
		if err != nil {
			fmt.Println(err.Error())
			countErrors++
		}
	}
	fmt.Println("Total errors:", countErrors)
}
ADeadlock error
B
error from worker 2
error from worker 4
Total errors: 2
C
error from worker 1
error from worker 3
error from worker 5
Total errors: 3
DTotal errors: 0
Attempts:
2 left
💡 Hint
Check which workers send errors and which send nil, and how many errors are counted.