Debugging is interactive: you pause a running program, inspect variables and step through code to find why something is wrong, usually on your machine. Logging is passive: the program writes records of what happened so you can understand behavior later, especially in production where you cannot attach a debugger. In Go, the main tools are Delve for debugging and the standard log/slog package for logging.
Use a debugger to answer "why is this value wrong right now?", and logs to answer "what happened, and when, in a system I cannot pause?".
Delve (dlv) is the standard Go debugger and powers the debugging features in VS Code (Go extension) and GoLand. It understands goroutines, channels and Go's runtime, which gdb handles poorly.
Install and start a session:
Useful commands inside the (dlv) prompt:
Go compiles with optimizations that can hide variables. dlv debug and dlv test build without optimizations for you. If you build the binary yourself and want accurate debugging, use go build -gcflags="all=-N -l".
Since Go 1.21 the standard library includes log/slog, a structured logger with levels and key-value attributes. It replaces most uses of the older log package, and it can output JSON for log aggregators.
The timestamps will differ on your machine. Good logging practice:
fmt.Sprintf, so logs can be filtered and searched.Debug for developer detail, Info for normal events, Warn for recoverable problems, Error for failures.logger.With("request_id", id)) so you can follow one operation across services.| Aspect | Debugging | Logging |
|---|---|---|
| Interaction | Interactive, pauses the program | Non-interactive, program keeps running |
| Where | Development, tests, reproducible bugs | Development and production |
| Output | Live inspection of state | A persistent record of events |
| Tools | Delve, IDE debuggers | log/slog, log, Zap, Zerolog |
| Overhead | Stops execution, not for live traffic | Small but continuous cost (I/O, volume) |
| Best for | Locating the cause of a known bug | Detecting problems, auditing, monitoring |
go test -run and Delve is the fastest loop for most bugs.go test -race and go run -race find data races that neither logs nor breakpoints show clearly.net/http/pprof and go tool pprof explain CPU and memory problems.go tool trace helps with scheduling and latency issues.goroutines in Delve or a goroutine dump.It is fine for a quick check, but remove it afterwards. log/slog or Delve is better for anything you will keep or need to repeat.
Yes. Run Delve in headless mode (dlv exec --headless --listen=:2345 --api-version=2) and connect from your IDE, but never expose that port publicly.
log/slog better than Zap or Zerolog?For most applications slog is enough and has no dependency. Zap and Zerolog can still be useful when you need extreme allocation efficiency or existing integrations.
Slightly. Avoid logging in tight loops, use levels, and keep expensive argument computation behind a level check such as logger.Enabled.
Use debugging (Delve, tests, the race detector) to investigate problems you can reproduce, and logging (log/slog with structured fields) to observe behavior over time, especially in production. Most real systems need both, plus metrics and traces for visibility across services.