Esta página se muestra en inglés. Una traducción al español está en curso.
ConfigsMay 28, 2026·3 min de lectura

Gofumpt — Stricter Go Code Formatter Beyond gofmt

Gofumpt is a stricter superset of gofmt that enforces additional formatting rules for Go code. It removes unnecessary blank lines, standardizes empty blocks, and ensures consistent grouping of declarations, producing cleaner and more uniform Go source files.

Listo para agents

Instalación lista para agent

Este activo puede instalarse después de elegir el runtime, revisar el plan y ejecutar el comando correspondiente.

Native · 98/100Política: permitir
Superficie agent
Cualquier agent MCP/CLI
Tipo
Skill
Instalación
Single
Confianza
Confianza: Established
Entrada
Gofumpt Formatter
Comando de instalación directa
npx -y tokrepo@latest install 4aefa6c7-5ad5-11f1-9bc6-00163e2b0d79 --target codex

Ejecutar después de confirmar el plan con dry-run.

Introduction

Gofumpt is a Go code formatter that builds on top of the standard gofmt with additional opinionated rules. Where gofmt intentionally leaves some formatting decisions open, gofumpt closes those gaps by enforcing stricter conventions, reducing style debates in code reviews and producing more uniform Go codebases.

What Gofumpt Does

  • Applies all standard gofmt formatting rules as a baseline
  • Removes unnecessary empty lines inside function bodies and blocks
  • Standardizes empty declaration blocks (var, const, type) to single-line form
  • Groups adjacent same-type declarations and removes redundant parentheses
  • Enforces consistent spacing around composite literals and short variable declarations

Architecture Overview

Gofumpt is implemented as a post-processor that first runs the standard gofmt formatting pass, then applies its own additional rules on the resulting AST. This architecture guarantees that gofumpt output is always a superset of gofmt: any file formatted by gofumpt is also valid gofmt output. The extra rules operate on the token-level positions in the AST, adjusting whitespace and newlines without changing semantics.

Self-Hosting & Configuration

  • Install with go install mvdan.cc/gofumpt@latest
  • Run gofumpt -w . to format all Go files in place
  • Use as a drop-in replacement for gofmt in editor save hooks
  • Configure VS Code by setting "gopls": {"gofumpt": true} in settings
  • Integrates with golangci-lint via the gofumpt linter option

Key Features

  • Strict superset of gofmt ensuring full backward compatibility
  • Deterministic output eliminates formatting debates in code reviews
  • Editor integration through gopls for format-on-save workflows
  • CI-friendly with -d (diff) and -l (list changed files) modes
  • Actively maintained with rules evolving alongside Go releases

Comparison with Similar Tools

  • gofmt — Go's standard formatter; gofumpt adds stricter rules on top
  • goimports — manages imports and formats; gofumpt focuses on code body formatting
  • golines — wraps long lines; gofumpt handles whitespace and blank lines
  • Prettier (JS) — opinionated formatter for JS; gofumpt is the Go equivalent in philosophy
  • Revive — lints for style violations; gofumpt automatically fixes formatting issues

FAQ

Q: Is gofumpt output compatible with gofmt? A: Yes, gofumpt is a strict superset. Any file formatted by gofumpt is also valid gofmt output, but not vice versa.

Q: How do I enable gofumpt in VS Code? A: Add "gopls": {"formatting.gofumpt": true} to your VS Code settings.json and gopls will use gofumpt on save.

Q: Will gofumpt change my code behavior? A: No, gofumpt only modifies whitespace and formatting. It never changes identifiers, logic, or semantics.

Q: Can I use gofumpt alongside golangci-lint? A: Yes, golangci-lint includes gofumpt as a configurable linter that checks and reports formatting differences.

Sources

Discusión

Inicia sesión para unirte a la discusión.
Aún no hay comentarios. Sé el primero en compartir tus ideas.

Activos relacionados