---
title: Explicit Error Handling
impact: CRITICAL
impactDescription: prevents silent failures and ensures robust error recovery
tags: go, error-handling, quality
---

## Explicit Error Handling

In Go, errors are values and must be handled explicitly. Ignoring errors using `_` or failing to check them leads to unpredictable state and silent failures.

**Incorrect (ignoring errors):**

```go
func processData(data string) {
    file, _ := os.Open("config.json") // Error ignored
    defer file.Close()
    
    bytes, _ := io.ReadAll(file) // Error ignored
    json.Unmarshal(bytes, &config) // Error ignored
}
```

**Correct (explicit checking):**

```go
func processData(data string) error {
    file, err := os.Open("config.json")
    if err != nil {
        return fmt.Errorf("failed to open config: %w", err)
    }
    defer file.Close()
    
    bytes, err := io.ReadAll(file)
    if err != nil {
        return fmt.Errorf("failed to read config: %w", err)
    }
    
    if err := json.Unmarshal(bytes, &config); err != nil {
        return fmt.Errorf("failed to parse config: %w", err)
    }
    
    return nil
}
```

**Benefits:**
- Prevents silent crashes and data corruption
- Provides clear trace of what went wrong
- Forces developers to think about failure paths
- Makes code easier to debug

**Tools:** errcheck, golangci-lint, staticcheck
