LinuxCommandLibrary

gofumpt

Enforce stricter formatting for Go code

TLDR

Format Go files

$ gofumpt -w [path/to/directory]
copy

[l]ist files whose formatting differs from gofumpt
$ gofumpt -l [path/to/directory]
copy

Report all [e]rrors
$ gofumpt -e [path/to/directory]
copy

Display [d]iffs
$ gofumpt -d [path/to/directory]
copy

Format Go files with stricter rules
$ gofumpt -extra [path/to/directory]
copy

Display [d]iffs with stricter rules
$ gofumpt -extra -d [path/to/directory]
copy

Display help
$ gofumpt [[-h|--help]]
copy

SYNOPSIS

gofumpt [-d] [-extra] [-l] [-s] [-w] [paths...]
Formats Go source files in place or lists differences.

PARAMETERS

-d
    Display diffs between original and reformatted source instead of rewriting files.
Useful for reviewing changes.

-extra
    Apply extra stricter formatting rules beyond standard gofmt.
Enabled by default in recent versions.

-l
    List files whose formatting differs from gofumpt's style.
Does not modify files.

-s
    Try to simplify code by removing redundant parentheses, etc.
Combines well with other flags.

-w
    Write reformatted source directly to files.
Default behavior unless -l or -d is used.

DESCRIPTION

gofumpt is a drop-in replacement for Go's standard gofmt tool, enforcing a stricter set of formatting rules to improve code consistency and readability. While gofmt ensures uniform style across Go projects, gofumpt goes further by disallowing common idioms that could be simplified or are considered suboptimal, such as redundant parentheses or overly complex expressions.

It applies all gofmt rules plus extras like banning trailing commas in multi-line slices, requiring 'go:generate' directives in specific positions, and enforcing blank lines around functions. This makes it ideal for teams wanting more rigorous style enforcement without manual linting.

Usage is identical to gofmt: pipe code or specify files/directories. It's particularly useful in CI/CD pipelines or editor integrations for automatic formatting. Developed to address gaps in standard formatting, it promotes "happy path" code that's easier to read and maintain. Install via go install; it's lightweight and fast.

CAVEATS

Not installed by default on Linux; requires Go toolchain. Mutually exclusive flags like -l/-d/-w may override each other. Only formats Go source (.go files).

INSTALLATION

Run go install mvdan.cc/gofumpt@latest to install the binary in $GOPATH/bin.
Add to PATH for system-wide use.

EDITOR INTEGRATION

Supports VS Code, Vim, Emacs via plugins like null-ls or efm-langserver.
Configure as gofumpt -l -w for lint+format.

HISTORY

Created by Daniel Martí (mvdan) in 2021 as a stricter gofumpt fork. Evolved from community demand for enhanced rules; v0.1.0 released Oct 2021. Now widely used in Go projects for CI formatting, with ongoing updates aligning to Go releases.

SEE ALSO

gofmt(1), goimports(1), golines(1)

Copied to clipboard