0
0
Goprogramming~20 mins

Returning errors in Go - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Go Error Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Go code with error handling?

Consider the following Go code that tries to open a file and returns an error if it fails. What will be printed when the file does not exist?

Go
package main

import (
	"fmt"
	"os"
)

func openFile(filename string) error {
	_, err := os.Open(filename)
	return err
}

func main() {
	err := openFile("nonexistent.txt")
	if err != nil {
		fmt.Println("Error occurred:", err)
	} else {
		fmt.Println("File opened successfully")
	}
}
AFile opened successfully
BError occurred: open nonexistent.txt: no such file or directory
CError occurred: <nil>
DCompilation error: missing return statement
Attempts:
2 left
💡 Hint

Think about what happens when os.Open tries to open a file that does not exist.

Predict Output
intermediate
2:00remaining
What error does this Go function return?

Look at this Go function that returns an error. What will be the output when calling checkValue(5)?

Go
package main

import (
	"errors"
	"fmt"
)

func checkValue(x int) error {
	if x < 10 {
		return errors.New("value too small")
	}
	return nil
}

func main() {
	err := checkValue(5)
	if err != nil {
		fmt.Println(err.Error())
	} else {
		fmt.Println("Value is fine")
	}
}
ACompilation error: undefined errors package
BValue is fine
Cvalue too small
Dnil
Attempts:
2 left
💡 Hint

Check the condition inside checkValue and what error is returned.

🔧 Debug
advanced
2:00remaining
Which option causes a compile-time error due to wrong error return?

Identify which code snippet will cause a compile-time error because it returns a wrong type instead of an error.

Afunc doSomething() error { return "error string" }
Bfunc doSomething() error { return nil }
Cfunc doSomething() error { return fmt.Errorf("fail") }
Dfunc doSomething() error { return errors.New("fail") }
Attempts:
2 left
💡 Hint

Remember that the function must return a value of type error.

Predict Output
advanced
2:00remaining
What is the output of this Go code using wrapped errors?

Consider this Go code that wraps an error. What will be printed?

Go
package main

import (
	"errors"
	"fmt"
)

func inner() error {
	return errors.New("inner error")
}

func outer() error {
	return fmt.Errorf("outer error: %w", inner())
}

func main() {
	err := outer()
	fmt.Println(err.Error())
}
Aouter error
Binner error
CCompilation error: unknown verb %w
Douter error: inner error
Attempts:
2 left
💡 Hint

Look at how %w is used in fmt.Errorf.

Predict Output
expert
3:00remaining
What is the output of this Go code using custom error type and error interface?

Analyze this Go code with a custom error type implementing the error interface. What will be printed?

Go
package main

import (
	"fmt"
)

type MyError struct {
	Code int
	Msg  string
}

func (e *MyError) Error() string {
	return fmt.Sprintf("Error %d: %s", e.Code, e.Msg)
}

func doTask(fail bool) error {
	if fail {
		return &MyError{Code: 404, Msg: "Not Found"}
	}
	return nil
}

func main() {
	err := doTask(true)
	if err != nil {
		switch e := err.(type) {
		case *MyError:
			fmt.Println("Custom error detected:", e.Error())
		default:
			fmt.Println("Other error:", err.Error())
		}
	} else {
		fmt.Println("Task succeeded")
	}
}
ACustom error detected: Error 404: Not Found
BOther error: Error 404: Not Found
CTask succeeded
DCompilation error: cannot use &MyError literal as error
Attempts:
2 left
💡 Hint

Check how the type assertion works with the custom error type.