A simple Go project to demonstrate robust error handling patterns including:
- ✅ Sentinel errors
- 🔄 Error wrapping with
%w - 🧩 Custom error types
- ⏳ Context-based timeouts
- 🔍 Error inspection with
errors.Isanderrors.As
The taskrunner simulates a basic task execution system. It reads a list of tasks from a JSON file, validates them, and processes each task within a deadline. This project is designed as a practical guide to idiomatic error handling in Go.
taskrunner/
├── go.mod
├── main.go
├── README.md
├── taskrunner
│ ├── processor.go
│ ├── reader.go
│ └── task.go
└── tasks.json
- Go 1.18 or higher installed
git clone https://github.com/0mynk/task_processor
cd task_processor
go mod tidy #optional
go run main.go[
{ "id": "task1", "value": 42 },
{ "id": "task2", "value": -5 },
{ "id": "task3", "value": 13 },
{ "id": "task4", "value": 42 },
{ "id": "task5", "value": 3 },
{ "id": "task6", "value": 1 },
{ "id": "task7", "value": 2 },
{ "id": "task8", "value": 22 }
]| Feature | Description |
|---|---|
| Sentinel Errors | Reusable, predefined error values like ErrFileNotFound, checked with errors.Is |
| Error Wrapping | Use %w in fmt.Errorf to preserve and trace error origins |
| Custom Error Types | Struct-based errors (ErrInvalidTask) to carry task-specific info |
| Context Cancellation | Timeout each task using context.WithTimeout |
| Error Unwrapping | Detect specific error types with errors.As and errors.Is |
Task "task1" timed out
Skipping invalid task "task2": negative value
Task "task3" timed out
Task "task4" timed out
Task "task5" completed successfully
Task "task6" completed successfully
Task "task7" completed successfully
Task "task8" timed out
- Add unit tests with the
testingpackage - Implement concurrency with goroutines and channels
- Add retries or backoff mechanisms
- Log errors using a structured logger like
zaporlogrus
MIT License — free to use, modify, and share.
Crafted as a learning tool for mastering Go's error handling. Feel free to fork, star, or open a pull request!