0
0
Goprogramming~10 mins

Error interface in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Error interface
Start
Call function returning error
Check if error is nil
Handle error
End
This flow shows how Go functions return errors and how to check and handle them.
Execution Sample
Go
package main
import (
 "errors"
 "fmt"
)
func doTask() error {
 return errors.New("something went wrong")
}
func main() {
 err := doTask()
 if err != nil {
  fmt.Println("Error:", err)
 } else {
  fmt.Println("Success")
 }
}
This code defines a function that returns an error and main checks and prints the error if present.
Execution Table
StepActionEvaluationResult
1Call doTask()doTask() returns errorerror with message 'something went wrong'
2Assign error to errerr = error objecterr holds error
3Check if err != nilerr is not nilCondition true
4Print error messagefmt.Println("Error:", err)Output: Error: something went wrong
5End programNo further codeProgram terminates
💡 Program ends after printing the error message because err was not nil
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
errnilerror object createderror object assignederror object checked (not nil)error object held
Key Moments - 3 Insights
Why do we check if err != nil before using err?
Because err is nil when there is no error, so checking err != nil ensures we only handle errors when they actually exist, as shown in step 3 of the execution_table.
What does errors.New("something went wrong") do?
It creates a new error object with the message 'something went wrong', which is returned by doTask() in step 1.
Why do we print err directly instead of err.Error()?
Printing err calls its Error() method automatically, so fmt.Println("Error:", err) prints the error message, as seen in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of err after step 2?
Anil
Bempty string
Cerror object with message 'something went wrong'
Dboolean false
💡 Hint
Check the 'After Step 2' column in variable_tracker for err
At which step does the program decide to print the error message?
AStep 3
BStep 4
CStep 1
DStep 5
💡 Hint
Look at the 'Action' and 'Result' columns in execution_table
If doTask() returned nil instead of an error, what would happen at step 3?
ACondition err != nil would be false
BCondition err != nil would be true
CProgram would crash
DError message would print anyway
💡 Hint
Refer to the 'Check if err != nil' row in execution_table and understand nil means no error
Concept Snapshot
Error interface in Go:
- Functions return error type to signal problems.
- Check if error is nil before handling.
- Use errors.New() to create errors.
- Print error directly calls Error() method.
- Always handle errors to avoid silent failures.
Full Transcript
This visual execution traces how Go's error interface works. The function doTask returns an error created by errors.New. The main function assigns this error to variable err. It then checks if err is not nil, meaning an error occurred. Since err holds an error, the program prints the error message. If err were nil, it would print success instead. This shows the common pattern of error handling in Go: call function, check error, handle error or continue. Variables and conditions are tracked step-by-step to clarify the flow.