Use when Go error handling with error wrapping, sentinel errors, and custom error types. Use when handling errors in Go applications.
View on GitHubTheBushidoCollective/han
jutsu-go
January 24, 2026
Select agents to install to:
npx add-skill https://github.com/TheBushidoCollective/han/blob/main/jutsu/jutsu-go/skills/go-error-handling/SKILL.md -a claude-code --skill go-error-handlingInstallation paths:
.claude/skills/go-error-handling/# Go Error Handling
Master Go's error handling patterns including error wrapping, sentinel
errors, custom error types, and the errors package for robust applications.
## Basic Error Handling
**Creating and returning errors:**
```go
package main
import (
"errors"
"fmt"
)
func divide(a, b float64) (float64, error) {
if b == 0 {
return 0, errors.New("division by zero")
}
return a / b, nil
}
func main() {
result, err := divide(10, 0)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("Result:", result)
}
```
**Using fmt.Errorf:**
```go
func processFile(filename string) error {
if filename == "" {
return fmt.Errorf("filename cannot be empty")
}
// Process file...
return nil
}
```
## Error Wrapping
**Wrapping errors with context (Go 1.13+):**
```go
import (
"errors"
"fmt"
"os"
)
func readConfig(path string) error {
_, err := os.Open(path)
if err != nil {
return fmt.Errorf("failed to read config: %w", err)
}
return nil
}
func main() {
err := readConfig("config.json")
if err != nil {
fmt.Println(err)
// Output: failed to read config: open config.json: no such file
}
}
```
**Unwrapping errors:**
```go
func handleError(err error) {
// Unwrap one level
unwrapped := errors.Unwrap(err)
if unwrapped != nil {
fmt.Println("Unwrapped:", unwrapped)
}
// Check if specific error is in chain
if errors.Is(err, os.ErrNotExist) {
fmt.Println("File does not exist")
}
}
```
## Sentinel Errors
**Defining and using sentinel errors:**
```go
package main
import (
"errors"
"fmt"
)
var (
ErrNotFound = errors.New("resource not found")
ErrUnauthorized = errors.New("unauthorized access")
ErrInvalidInput = errors.New("invalid input")
)
func getUser(id int) (string, error) {
if id < 0 {
return "", ErrInvalidInput
}
if id == 0 {