gofumpt

repository·master·Indexed 26 days ago

https://github.com/mvdan/gofumpt

A stricter formatter for Go code that acts as a drop-in replacement for gofmt. It enforces additional formatting rules to improve code consistency while remaining backwards compatible with gofmt output. Requires Go 1.25 or later.

Tokens
1.3K
Snippets
9
Records
16
Agent score
38%

What's inside gofumpt

  1. Configure gofumpt in Visual Studio Code

    master

    To use gofumpt in VS Code, enable the language server and configure the gopls settings to use gofumpt formatting.

    "go.useLanguageServer": true,
    "gopls": {
    	"formatting.gofumpt": true,
    },
  2. Configure gofumpt in Sublime Text

    master

    Install the Sublime Text LSP extension and enable gofumpt in the LSP package settings, ensuring lsp_format_on_save is true.

    "lsp_format_on_save": true,
    "clients":
    {
    	"gopls":
    	{
    		"enabled": true,
    		"initializationOptions": {
    			"gofumpt": true,
    		}
    	}
    }
  3. Configure gofumpt in GoLand

    master

    GoLand requires a custom File Watcher since it does not use gopls. After installing gofumpt, add a new File Watcher with a Custom Template using these settings:

    • File Types: .go files
    • Scope: Project Files
    • Program: Path to your gofumpt executable
    • Arguments: -w $FilePath$
    • Output path to refresh: $FilePath$
    • Working directory: $ProjectFileDir$
    • Environment variables: GOROOT=$GOROOT$;GOPATH=$GOPATH$;PATH=$GoBinDirs$

    Note: Disable all checkboxes in the 'Advanced' section to avoid unnecessary runs.

  4. Debug gofumpt formatting with //gofumpt:diagnose

    master

    To report a formatting bug or get debugging information, insert the //gofumpt:diagnose comment into your Go file. When you run gofumpt on that file, the comment will be rewritten to include version and language information.

    //gofumpt:diagnose
    package p
  5. Display formatting diffs with -d

    master
    Use the -d flag to see the differences between the current file content and the formatted output without modifying the files. This is useful for CI/CD pipelines to check if code adheres to the style guide. When -d is used and differences are found, the program exits with a non-zero exit code.
  6. Enable extra gofumpt rules via -extra

    master
    You can enable additional formatting rules that are not part of the default set using the -extra flag. This allows for more aggressive formatting, such as grouping parameters or specific return statement styles. The available rules are passed as a comma-separated list.