Linux command
goimports 命令
文件
复制后可按需替换文件名、目录或参数。
常用示例
Format and fix imports in a file
goimports [file.go]
Fix imports and write to file
goimports -w [file.go]
Show diff of changes
goimports -d [file.go]
List files with import issues
goimports -l [file.go]
Separate local packages
goimports -local [github.com/myorg] [file.go]
Format all files in directory
goimports -w .
说明
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.
参数
- -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.
FAQ
What is the goimports command used for?
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.
How do I run a basic goimports example?
Run `goimports [file.go]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -d do in goimports?
Display diffs instead of rewriting.