LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

goimports

Go import line management and formatting

TLDR

Format and fix imports in a file
$ goimports [file.go]
copy
Fix imports and write to file
$ goimports -w [file.go]
copy
Show diff of changes
$ goimports -d [file.go]
copy
List files with import issues
$ goimports -l [file.go]
copy
Separate local packages
$ goimports -local [github.com/myorg] [file.go]
copy
Format all files in directory
$ goimports -w .
copy

SYNOPSIS

goimports [flags] [path ...]

DESCRIPTION

goimports updates Go import lines by adding missing imports and removing unreferenced ones. It also formats code in the same style as gofmt, making it a drop-in replacement.Imports are grouped with standard library first, then third-party packages, and sorted alphabetically within each group. The -local flag allows separating project-specific imports into their own group.

PARAMETERS

-d

Display diffs instead of rewriting.
-l
List files whose imports differ.
-w
Write result to source file.
-local prefix
Comma-separated prefixes for local packages.
-e
Report all errors (not just the first 10 on different lines).
-format-only
Do not fix imports; only format the code. In this mode goimports is effectively gofmt with grouped imports.
-v
Verbose logging.
-srcdir dir
Choose imports as if source code is from the specified directory.

SEE ALSO

gofmt(1), go-fmt(1), gofumpt(1)

Copied to clipboard
Kai