Configs2026年5月28日·1 分钟阅读

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.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Gofumpt Formatter
直接安装命令
npx -y tokrepo@latest install 4aefa6c7-5ad5-11f1-9bc6-00163e2b0d79 --target codex

先 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

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产