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
brew install magicdrive/tap/goreg
brew upgrade goreg # later, to update itDownload the script, read it if you like, then run it with sh:
curl -fsSLO https://raw.githubusercontent.com/magicdrive/goreg/main/install.sh
sh install.sh # the latest release, into ~/.local/bin
"$HOME/.local/bin/goreg" --version # prints: goreg version vX.Y.Zsh install.sh # install the latest release
sh install.sh --version v1.3.12 # install a given release (older ones too)
sh install.sh --upgrade # switch to the latest stable release
sh install.sh --upgrade --version v1.3.12 # switch to a given release (also a downgrade)
sh install.sh --dir "$HOME/.local/bin" # choose the directory (this is the default)
sh install.sh --help- Where:
$HOME/.local/bin/goregby default;--dir DIRinstallsDIR/goregand createsDIRif needed (spaces inDIRare fine). It never usessudoand never changesPATHor shell start-up files: if the script says the directory is not inPATH, add it, e.g.export PATH="$HOME/.local/bin:$PATH"in~/.profile. For a system directory such as/usr/local/bin, run it as a user who can write there (it does not ask forsudoitself). - Versions: without
--versionit installs the latest stable release (pre-releases are not picked).--versiontakes a release tag,v1.3.12or1.3.12, newer or older than the installed one; a version that does not exist is an error, never another version.--upgradeis the same run, named for what you mean. Running it again with the installed version changes nothing. Every goreg release can be installed (v0.0.1 and later). - Updating: run
sh install.sh --upgradeagain (with the same--dirif you used one). - Checks: the archive must match the SHA-256 listed in the release's checksum file
(
goreg_checksums.txt); a missing or malformed checksum file stops the install. Only thegoregbinary is taken out of the archive, after its entries have been checked (no absolute paths,.., links or devices). The new binary must run and report the version asked for before it replaces the old one in a single rename: a failed or interrupted run leaves the installedgoregas it was. - Security: every request starts at github.com, over https, and redirects (GitHub sends the
downloads to its asset host) are followed only to https. The checksum file comes from the same GitHub release as the archive, so it catches a
broken or altered download, but not a release that was itself replaced on GitHub. For a
stronger check, verify the archive's build provenance with the
GitHub CLI before you trust it:
gh attestation verify goreg_<version>_<os>_<arch>.tar.gz -R magicdrive/goreg(releases from v1.3.11 on have these attestations). The command above runs the script from themainbranch; to run a fixed script, download it from a release tag instead (https://raw.githubusercontent.com/magicdrive/goreg/<tag>/install.sh; the tags from v1.4.0 on have it). Piping it straight intosh(curl ... | sh) also works, but then you run what you have not seen. - Homebrew: the script refuses Homebrew's directories (its prefix, e.g.
/opt/homebrew, or/usr/localon an Intel Mac) and does not replace a symbolic link such as one Homebrew made. Keep one way of installing: with Homebrew, upgrade withbrew upgrade goreg; the script warns if anothergoregcomes first inPATH. - Uninstall: delete the file:
rm "$HOME/.local/bin/goreg"(orDIR/goreg). - Needs: a POSIX
sh,curl(or GNU Wget 1.17+; BusyBox wget is refused because it does not check certificates, and wget2 is not supported),tar,gzip, andsha256sum,shasumoropenssl. A proxy set inhttps_proxyis used.INSTALLER_TIMEOUT(seconds, default 60) is how long it waits for a connection or more data. - Windows: the script does not run on Windows (Git Bash, MSYS2 and Cygwin are refused). Use
the
goreg_<version>_windows_<arch>.tar.gzof the Releases page (it holdsgoreg.exe). Inside WSL, the Linux instructions apply. - Troubleshooting: not writable — choose a directory of your own with
--dir; not in PATH — see above; does not run in DIR (noexec) — choose a directory on another file system; SHA-256 mismatch or rejected — nothing was installed; try again later and report it if it persists; release not found — check the version on the Releases page; inside the Homebrew prefix — usebrew upgrade goreg, or another--dir.
git clone https://github.com/magicdrive/goreg.git
cd goreg
make build # needs Go (see go.mod); the binary is build/goregDownload an archive from the Releases page:
goreg_<version>_<os>_<arch>.tar.gz for macOS (amd64, arm64), Linux (386, amd64, arm64) and
Windows (386, amd64, arm64), with goreg_checksums.txt (SHA-256) to check them against.
goreg [OPTIONS] <file-name.go>
goreg init| Subcommand | Description |
|---|---|
init |
Create a default goreg.toml configuration file in the current directory. |
| 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.
You can quickly create a default goreg.toml configuration file using the init subcommand:
goreg initThis will create a goreg.toml file in the current directory with default settings. If a goreg.toml file already exists, the command will not overwrite it and will display an error message.
[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.gogoreg -w file.gogoreg -l myproject/module file.gogoreg -o "std,org,thirdparty,local" file.gogoreg -m file.gogoreg -a file.goCopyright (c) 2025 Hiroshi IKEGAMI
This project is licensed under the MIT License.