Claude Hookbook

Hooks / formatting

gofmt after Go edit

Runs gofmt -w on Go files after Claude edits them.

PostToolUseEdit|Writelow riskneeds manual check

What it does

Gates on .go files and runs gofmt -w. gofmt ships with the Go toolchain, so there is nothing extra to install beyond Go itself.

When it runs

EventPostToolUse
MatcherEdit|Write
Scopeproject
Platformsmacos, linux
Timeout30s
Requiresjq go
Failure modeobserve
Verified againstClaude Code hooks reference, event/matcher schema checked 2026-09-10
Test statusneeds manual check - run the test steps below before relying on it

Config

Merge this into .claude/settings.json - or use the generator to combine hooks.

{
  "hooks": {
    "PostToolUse": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/gofmt-after-edit.sh",
            "timeout": 30
          }
        ],
        "matcher": "Edit|Write"
      }
    ]
  }
}

Script

Save as .claude/hooks/gofmt-after-edit.sh and chmod +x it.

#!/bin/bash
# gofmt-after-edit: run after Claude edits a file. Reads hook JSON from stdin.
set -u
file=$(jq -r '.tool_input.file_path // empty')
[ -z "$file" ] && exit 0
[ -f "$file" ] || exit 0
case "$file" in
*.go) ;;
  *) exit 0 ;;
esac
if ! command -v gofmt >/dev/null 2>&1; then
  exit 0  # formatter not installed; stay silent
fi
gofmt -w "$file" >/dev/null 2>&1
exit 0

Install

  1. Save the script to .claude/hooks/gofmt-after-edit.sh and run: chmod +x .claude/hooks/gofmt-after-edit.sh
  2. Install Go - gofmt ships with the toolchain, so there is no separate formatter install. jq is required too.
  3. Add the config fragment to .claude/settings.json (or use the generator).

Test it

Sources

Pro Pack (later)

Framework bundles, cross-platform scripts, tested blocking policies, and a test harness. Free recipes and the generator stay free.

Waitlist opens soon.