# error: cgo: C function returned error: errno 2

- **ID:** `go/cgo-errno-handling`
- **Domain:** go
- **Category:** system_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

C function sets `errno` on failure, but Go code does not check it, leading to silent failures.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 1.18 | active | — | — |
| 1.19 | active | — | — |

## Workarounds

1. **Check return value and use `C.CString` to get error message** (90% success)
   ```
   ret := C.someCFunction()
if ret != 0 {
    errno := C.errno
    errStr := C.GoString(C.strerror(errno))
    return fmt.Errorf("C error: %s", errStr)
}
   ```
2. **Use `cgo.Handle` to capture error in a thread-safe way** (80% success)
   ```
   // Not directly applicable; use mutex to protect errno reading
   ```

## Dead Ends

- **Ignoring the return value of C function** — No error handling at all. (90% fail)
- **Using `C.errno` directly without thread safety** — errno is thread-local in C, but Go may not preserve it. (60% fail)
