0
0
Goprogramming~10 mins

Custom error types in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Custom error types
Define struct for error
Implement Error() method
Create error instance
Return or use error
Check error type with type assertion
Handle specific error
End
Create a struct with Error() method, use it as error, then check type to handle specifically.
Execution Sample
Go
package main
import "fmt"
type MyError struct { Msg string }
func (e MyError) Error() string { return e.Msg }
func doSomething() error { return MyError{"Oops!"} }
func main() {
 err := doSomething()
 if e, ok := err.(MyError); ok {
  fmt.Println("Custom error:", e.Error())
 } else {
  fmt.Println("Other error")
 }
}
Defines a custom error type, returns it, and checks its type to print a message.
Execution Table
StepActionEvaluationResult
1Define MyError structN/AMyError type created with Msg field
2Implement Error() methodN/AMyError satisfies error interface
3Call doSomething()Returns MyError{"Oops!"}err holds MyError instance
4Type assert err to MyErrorerr.(MyError)ok is true, e is MyError{"Oops!"}
5Print custom error messagee.Error()Output: Custom error: Oops!
6End programN/AProgram exits normally
💡 Program ends after printing custom error message
Variable Tracker
VariableStartAfter Step 3After Step 4Final
errnilMyError{"Oops!"}MyError{"Oops!"}MyError{"Oops!"}
eN/AN/AMyError{"Oops!"}MyError{"Oops!"}
okN/AN/Atruetrue
Key Moments - 3 Insights
Why do we need to implement the Error() method on MyError?
Because the Error() method makes MyError satisfy the error interface, allowing it to be used as an error type (see execution_table step 2).
What does the type assertion err.(MyError) do?
It checks if err holds a MyError value and extracts it if true, as shown in execution_table step 4.
Why do we check the 'ok' variable after type assertion?
'ok' tells us if the assertion succeeded; if false, err is not MyError and we handle it differently (execution_table step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the value of 'ok' after the type assertion?
Afalse
Bnil
Ctrue
DMyError{"Oops!"}
💡 Hint
Check the 'Evaluation' and 'Result' columns at step 4 in execution_table.
At which step does the program print the custom error message?
AStep 4
BStep 5
CStep 3
DStep 6
💡 Hint
Look for the step where fmt.Println outputs the message in execution_table.
If the Error() method was missing from MyError, what would happen?
ACompilation error because MyError does not implement error interface
BRuntime panic when returning MyError
CMyError would still be used as error without issues
DThe program would print 'Other error'
💡 Hint
Recall that implementing Error() is required to satisfy the error interface (see key_moments).
Concept Snapshot
Custom error types in Go:
- Define a struct to hold error info
- Implement Error() string method on it
- Return instance as error
- Use type assertion to check error type
- Handle specific errors differently
Full Transcript
This example shows how to create a custom error type in Go by defining a struct MyError with a message field. We implement the Error() method so MyError satisfies the error interface. The function doSomething returns a MyError instance as an error. In main, we receive this error and use a type assertion to check if it is MyError. If yes, we print a custom message. The execution table traces each step: defining the struct, implementing the method, returning the error, asserting its type, and printing the message. Variables err, e, and ok track the error value and assertion success. Key moments clarify why Error() is needed, how type assertion works, and why we check the assertion result. The quiz tests understanding of these steps and consequences of missing Error(). This helps beginners see how custom errors work in Go step-by-step.