Yet another alternate goimports
tool.
goreg is a tool for formatting Go imports while maintaining a stable order. It organizes imports into four distinct groups:
- Standard Library
- Third-Party Libraries
- Organization Modules (optional)
- Local Project Imports
You can install goreg
using go install
:
go install github.com/magicdrive/goreg@latest
Alternatively, you can download a pre-built binary from the Releases page.
goreg [OPTIONS] <file-name.go>
Option | Description |
---|---|
-h , --help |
Show this help message and exit. |
-v , --version |
Show version information. |
-w , --write |
Write the formatted output directly to the file. (optional) |
-l , --local <local_module> |
Specify the local module path, typically the project's module name. Used to determine whether an import is local. (optional) |
-o , --order <group_order> |
Specify the order of import groups. Default: "std,thirdparty,organization,local" . Example: "stdlib,3rd,org,local" |
-n , --organization <org_path> |
Specify the module path of your organization. If specified, it groups imports that start with this prefix separately. (optional) |
-m , --minimize-group |
Do not separate import groups when an alias is present. (optional) |
-a , --sort-include-alias |
Sort imports with aliases within their respective groups. (optional) |
-r , --remove-import-comment |
Remove the comments in the import. (optional) |
Argument | Description |
---|---|
<file-name.go> |
The target Go file to be formatted. |
<local_module> |
The local module path, typically the project's module name. (optional) |
<org_path> |
The organization module path. If specified, it groups imports that start with this prefix separately. (optional) |
<group_order> |
Defines the order in which import groups are arranged. Must include all four: std , thirdparty , organization , and local . Example: "stdlib,3rd,org,local" |
Enviroment | Description |
---|---|
GOREG_NOT_USE_CONFIGFILE |
if anything other than "" is set, goreg.toml will not be searched for. |
goreg supports configuration via a goreg.toml
file. This allows users to define default options instead of specifying them via CLI arguments every time.
[general]
write = false # Whether to write the formatted output to the file.
[import]
local_module = "myproject" # Defines the local module path. If blank, it will be automatically guessed.
organization_module = "github.com/myorg" # Defines the organization's module path.
order = "std,thirdparty,organization,local" # Specifies the order of import groups.
[format]
minimize_group = false # Do not separate import groups when an alias is present.
sort_include_alias = false # Sort imports with aliases within their respective groups.
remove_import_comment = false # Remove comments in the import.
goreg will automatically search for goreg.toml
in the current directory and its parent directories. If no configuration file is found, it will check ~/.config/goreg/goreg.toml
as a fallback.
To override settings from the configuration file, you can specify options via CLI arguments.
goreg file.go
goreg -w file.go
goreg -l myproject/module file.go
goreg -o "std,org,thirdparty,local" file.go
goreg -m file.go
goreg -a file.go
This project is licensed under the MIT License.