goimports is a command-line formatter for Go source code. It extends gofmt-style formatting by maintaining import declarations at the same time, adding packages required by the source and removing imports that are no longer referenced. This makes it useful in editor save hooks and automated source-maintenance workflows where formatting and import cleanup should happen together.
The tool can process files, directory trees, or source supplied through standard input. Import selection is context-aware, so it can resolve imports according to the effective location of the source rather than simply rearranging existing lines.
This is free and open source software.
Key Features
- Adds missing import declarations required by Go source code.
- Removes imports that are no longer referenced.
- Formats code in the same general style as gofmt.
- Can list files whose formatting would change.
- Supports writing formatted output back to source files.
- Can display unified diffs instead of rewriting files.
- Accepts input from standard input, files, and directory trees.
- Supports a source-directory override for context-sensitive import resolution.
- Can place configured local import prefixes after third-party packages.
- Offers a format-only mode that leaves import selection unchanged.
Website: github.com/golang/tools/tree/master/cmd/goimports
Support:
Developer: The Go Authors
License: BSD 3-Clause License
goimports is written in Go. Learn Go with our recommended free books and free tutorials.
Related Software
| Go Code Formatters | |
|---|---|
| goimports-reviser | Sorts and groups imports by standard, third-party and project packages |
| gofumpt | Enforces stricter styling rules for cleaner, consistent source |
| gofmt | Applies standardised indentation, spacing and source layout |
| golines | Breaks lengthy statements into readable, shorter lines |
| goimports | Automatically adds missing imports and removes unused ones |
| gocondense | Combines multiline constructs to reduce unnecessary vertical space |
Read our verdict in the software roundup.
LinuxLinks helps you discover the best free and open source software for Linux. Explore our extensive collection of recommended applications, including alternatives to popular proprietary software.We also publish software reviews, Linux tutorials, hardware reviews, free programming books and articles covering the wider open source community. Browse our Linux resources to discover more. Know a useful free and open source Linux application we haven't covered? Suggest a program for inclusion. Has your software been featured on LinuxLinks? Display one of our Featured on LinuxLinks badges on your website or GitHub README. |


Please read our Comment Policy before commenting.