go.mod Formatter

Format and validate Go module files. Sort require blocks, align versions, detect duplicates, and inspect module directives instantly.

About This Tool

The go.mod Formatter is a free browser-based tool that formats and validates Go module files. Paste your go.mod content, and the tool will sort require blocks alphabetically, align version numbers into neat columns, separate direct and indirect dependencies, and produce clean, consistent output that follows Go community conventions.

The formatter parses all standard go.mod directives: module, go version, toolchain, require, replace, exclude, and retract. It detects common issues like duplicate require entries (which can appear after manual edits or bad merges) and incompatible major version suffixes in import paths. The remove-duplicates option automatically deduplicates entries, keeping the last occurrence.

If you work with JSON configuration in Go projects, try the JSON to Go Struct converter to generate idiomatic Go types from JSON. For formatting YAML configuration files used alongside Go services, check the YAML Formatter. And if you need to format JSON data, the JSON Formatter provides similar formatting and validation capabilities.

All processing runs entirely in your browser. No go.mod content, module paths, or dependency information is ever sent to any server. This tool is safe to use with private module paths and internal dependency information.

How to Use

  1. Paste your go.mod content into the input textarea, or click Load Sample to see an example.
  2. Optionally check or uncheck Remove duplicates to control whether duplicate require entries are automatically deduplicated.
  3. The tool automatically formats the input as you type, producing aligned and sorted output in the right panel.
  4. Review the Module Info section below the output for a summary of the module name, Go version, dependency counts, and any detected issues.
  5. Inspect the Direct Dependencies and Indirect Dependencies tables for a clean view of all require entries.
  6. Click Copy or press Ctrl+Shift+C to copy the formatted output to your clipboard.
  7. Click Download to save the formatted output as a go.mod file.

Popular go.mod Examples

View all go.mod examples →

FAQ

What does the formatter do to my go.mod?

It sorts require entries alphabetically within their blocks, aligns version numbers into columns, separates direct and indirect dependencies into distinct require blocks, and ensures consistent indentation with tabs (as per Go convention). Replace, exclude, and retract directives are preserved and formatted consistently.

Does it run go mod tidy?

No. This tool only reformats and validates the text structure of go.mod. It does not resolve dependencies, fetch modules, or modify the dependency graph. For full dependency resolution, run `go mod tidy` in your Go project. This tool is useful for quick formatting and inspection without needing a Go installation.

How does duplicate detection work?

The formatter scans all require entries and flags any module path that appears more than once. When the 'Remove duplicates' option is enabled, it keeps only the last occurrence of each module path. Duplicates commonly appear after manual edits, merge conflicts, or copy-paste errors.

What is an incompatible major version?

Go modules enforce that import paths for major versions v2 and above include a version suffix (e.g., github.com/foo/bar/v3). If the import path says /v3 but the version is v2.1.0, the formatter flags this as an incompatible major version error.

Does it support replace directives with local paths?

Yes. Replace directives can point to local file paths (e.g., `replace github.com/foo/bar => ../local-bar`) or to other module versions. The formatter preserves both forms and aligns them consistently.

Is my data safe?

Yes. All formatting and validation runs entirely in your browser using JavaScript. No data — including module paths, dependency versions, or any go.mod content — is ever sent to any server. You can verify this by checking the Network tab in your browser's developer tools while using the tool.

Does it preserve comments?

The formatter preserves the `// indirect` comments that Go uses to mark indirect dependencies. General comments at the top of the file or inline comments on other directives are not currently preserved during formatting, as the tool regenerates the output from parsed data.

Related Tools