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.
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") } }
| Step | Action | Evaluation | Result |
|---|---|---|---|
| 1 | Call doTask() | doTask() returns error | error with message 'something went wrong' |
| 2 | Assign error to err | err = error object | err holds error |
| 3 | Check if err != nil | err is not nil | Condition true |
| 4 | Print error message | fmt.Println("Error:", err) | Output: Error: something went wrong |
| 5 | End program | No further code | Program terminates |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|---|
| err | nil | error object created | error object assigned | error object checked (not nil) | error object held |
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.