0
0
Goprogramming~10 mins

Returning errors in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Returning errors
Start function
Perform operation
Error occurred?
NoReturn result, nil
Yes
Return zero value, error
Function ends
The function starts, does some work, checks if an error happened, and returns either the result with no error or a zero value with an error.
Execution Sample
Go
func divide(a, b int) (int, error) {
    if b == 0 {
        return 0, fmt.Errorf("cannot divide by zero")
    }
    return a / b, nil
}
This function divides two numbers and returns an error if dividing by zero.
Execution Table
StepInput (a,b)Condition (b==0)ActionReturn values
1a=10, b=2falseCalculate 10/25, nil
2a=10, b=0trueReturn error0, error("cannot divide by zero")
💡 Function returns after either successful division or error detection.
Variable Tracker
VariableStartAfter Step 1After Step 2
a101010
b2 or 020
errornilnilerror("cannot divide by zero")
resultundefined50
Key Moments - 2 Insights
Why do we return 0 along with the error instead of just the error?
In Go, functions must return all declared return values. When an error occurs, we return the zero value for the result (here 0) and the error. See execution_table row 2.
What does 'nil' mean when returned as the error?
'nil' means no error happened. In execution_table row 1, error is nil, so the division was successful.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is returned when b=0?
A5, nil
B0, error("cannot divide by zero")
C10, nil
D0, nil
💡 Hint
Check execution_table row 2 for b=0 case.
At which step is the condition b==0 false?
ABoth steps
BStep 2
CStep 1
DNeither step
💡 Hint
Look at the Condition column in execution_table.
If we change the return to 'return a / b, error' without checking b==0, what happens?
AProgram panics at runtime if b=0
BReturns zero and nil error
CAlways returns error
DCompiles but never runs
💡 Hint
Think about division by zero in Go and error handling in execution_table.
Concept Snapshot
func functionName(...) (resultType, error) {
  if errorCondition {
    return zeroValue, error
  }
  return result, nil
}

- Return zero value with error on failure
- Return result with nil error on success
- Always return both values
Full Transcript
This example shows how a Go function returns errors. The function divide takes two integers and returns an integer result and an error. It checks if the divisor is zero. If yes, it returns zero and an error message. If no, it returns the division result and nil error. The execution table shows two cases: dividing 10 by 2 returns 5 and no error; dividing 10 by 0 returns zero and an error. Variables a and b keep their input values. The error variable is nil when no error occurs and holds an error message otherwise. Returning zero with an error is required because Go functions must return all declared values. Nil means no error. If we skip the error check and divide by zero, the program will crash at runtime.