goimports
Automatically update Go import lines
TLDR
Display the completed import source file
Write the result back to the source file instead of stdout
Display diffs and write the result back to the source file
Set the import prefix string after 3rd-party packages (comma-separated list)
SYNOPSIS
goimports [flags] [path ...]
PARAMETERS
-local pattern
Specifies a comma-separated list of Go package prefixes considered 'local' to the current project. Imports with these prefixes are grouped separately, typically after standard library and third-party imports.
-w
Write result to the source file instead of standard output. This flag is crucial for in-place modifications.
-d
Display diffs instead of writing to the file. This shows what changes goimports would make without applying them.
-l
List files whose formatting would change. Useful for continuous integration checks.
-e
Report all errors, not just the first one.
-v
Enable verbose logging.
-f file | dir
Specify a file or directory to process. If omitted, goimports processes the current directory ('.') if no paths are given, or standard input.
-cpuprofile file
Write a CPU profile to the specified file.
-memprofile file
Write a memory profile to the specified file.
-srcdir dir
Search for source files in the specified directory.
-tabwidth n
Set tab width to n (default 8).
-tabindent
Indent with tabs instead of spaces.
DESCRIPTION
goimports is a Go tool that automatically adds missing import paths and removes unused ones for Go source files. It also formats your Go code consistent with gofmt's style. This ensures that Go source files are well-formatted and have correctly managed import statements, which is crucial for code readability and compilation. It can process individual files, directories, or standard input, making it highly versatile for use in development workflows, build systems, or text editors. By automating import management, goimports significantly reduces manual effort and potential errors associated with dependency declarations in Go projects.
CAVEATS
When using the -w flag, goimports modifies files in place; it is always recommended to use version control. It operates on syntactically valid Go code; severe parsing errors may cause it to fail. It relies on the Go module system and environment configuration (e.g., GOPATH) to resolve import paths, which must be correctly set up.
INTEGRATION WITH IDES/EDITORS
goimports is widely integrated into popular Go development environments and text editors (e.g., VS Code, GoLand, Vim, Emacs) through language server protocols or specific plugins. This allows for automatic import management and formatting on save, greatly enhancing developer productivity.
DETERMINISM
The output of goimports is deterministic, meaning that for the same input and configuration, it will always produce the same output. This is crucial for maintaining consistent code style across teams and for use in automated build or CI/CD pipelines.
HISTORY
goimports emerged as a powerful extension to the standard gofmt tool within the Go ecosystem. While gofmt focuses purely on code formatting, goimports adds the intelligence of managing import statements automatically. It was developed to simplify the often tedious task of adding and removing package imports manually, especially in larger projects with many dependencies. Its integration into various Go IDEs and editors has made it an indispensable tool for Go developers, promoting consistent code style and correct dependency management from the early stages of development.