This tool is EXPERIMENTAL! We strongly recommend to back up (or use git to commit your changes) before to try it.
This tool will "reorder" your sources:
- Alphabetic reorder your types, vars, const, methods/functions and constructors (constructors will be also placed above methods)
- Place methods and constructors below the
type
definition - Output the result or write or even generate a patch file
- Use the default internal "gofmt" or use your own. That means that
goreorder
can be used in place of your formatting tool
There are several possibilities:
- If you have "go" on your machine, simply install using (you can replace "latest" by a known tag):
go install github.com/metal3d/goreorder/cmd/goreorder@latest
- Visit the release page to download the desired version (to place inside your
$PATH
) - Use the installer:
curl -sSL https://raw.githubusercontent.com/metal3d/goreorder/main/repo-tools/install.sh | bash -s
The installer script detects if you are launching it as root or standard user and installs the tool in:
$HOME/.local/bin
or$HOME/bin
for standard user if it exists (it fails if one of these paths doesn't exist)/usr/local/bin
if you're root user (or using sudo)
You can also get this repository and build it with the Makefile
:
git clone git@github.com:metal3d/goreorder.git
cd goreorder
make install
goreorder reorders the types, methods... in a Go
source file. By default, it will print the result to stdout. To allow goreorder
to write to the file, use the -write flag.
Usage:
goreorder [flags] [file.go|directory|stdin]
goreorder [command]
Examples:
$ goreorder reorder --write --reorder-types --format gofmt file.go
$ goreorder reorder --diff ./mypackage
$ cat file.go | goreorder reorder
Available Commands:
completion Generates completion scripts
help Help about any command
print-config Print the configuration
reorder Reorder vars, consts, stucts/types/interaces, methods/functions and constructors in a Go source file.
Flags:
-h, --help help for goreorder
-v, --version version for goreorder
Use "goreorder [command] --help" for more information about a command.
The reorder subcommand is the more important part:
Reorder vars, consts, stucts/types/interaces, methods/functions and constructors in a Go source file.
Usage:
goreorder reorder [flags] [file.go|directory|stdin]
Flags:
-d, --diff Print diff/patch format instead of rewriting the file
-f, --format string Format tool to use (gofmt or goimports) (default "gofmt")
-h, --help help for reorder
-o, --order strings Order of elements when rewriting. You can omit elements, in which case they will
be placed in the default order after those you have specified.
There are two specific cases: main and init - if they are not specified in the list,
then they are considered to be functions and will be ordered as such. If you do specify
them, then they will be positioned in the source code in the place you have specified.
- Allowed values are: main, init, const, var, interface, type, func
- Default order is: const,var,interface,type,func
-r, --reorder-types Reordering types in addition to methods
-v, --verbose Verbose output
-w, --write Write result to (source) file instead of stdout
You can create a .goreorder
file containing configuration at the root of your project. Use the goreorder print-config
command (you can redirect the output to the .goreorder
file).
Warning,
print-config
shows the current configuration. If the file doesn't exist, so the default values are displayed. If it exists, so the current values are displayed. To reset the file, remove it and rerun theprint-config
subcommand.
By default, main()
and init()
functions are part of the functions. So they are sorted with the others functions. If you don't want this behavior, you can specify where to place them using the --order
argument or using the .goreorder
configuration file.
For example, in command line:
goreorder reorder --order const,var,init,main ./
Or in configuration:
format: gofmt
write: true
verbose: true
reorder-types: true
diff: false
order:
- const
- var
- init
- main
If your system provides diff
and patch
command, it is safier to use the --diff
option to geneate
a patch
file. This file can then be used to apply changes, and to revert your changes if it fails.
Example:
goreorder reorder --diff ./ > reorder.patch
# try to apply
patch -p1 --dry-run < ./reorder.patch
# really apply
patch -p1 < ./reorder.patch
# revert the changes
patch -p1 -R < ./reorder.patch
The released binaries are signed with GPG. If you want to verify that the release comes from this repository and was built by the author:
## Optional, you can get and trust the owner GPG key
# import the key from github
# install jq before (apt install -y jq, dnf install -y jq, ...)
gpg --import <(curl -s https://api.github.com/users/metal3d/gpg_keys | jq -r '.[0].raw_key')
# or use keyserver
_KEY="483493B2DD0845DA8F21A26DF3702E3FAD8F76DC"
gpg --keyserver hkps://keys.openpgp.org/ --recv-keys ${_KEY~15}
## optional, trust owner key
_KEY="483493B2DD0845DA8F21A26DF3702E3FAD8F76DC"
echo ${_KEY}:6: | gpg --import-ownertrust
## Binary signature verification
# get the signature file (.asc) of the right binary
_REL="goreorder-linux-amd64"
_SIGNURL=https://github.com/metal3d/goreorder/releases/download/${_REL}.asc
curl ${_SIGNURL} -o /tmp/goreorder.asc
unset _SIGNURL _REL
# get or set the path to the binary file you downloaded / installed
# _GOREORDERBIN=/path/to/the/binary
_GOREORDERBIN=$(command -v goreorder)
# check the signature
gpg --verify /tmp/goreorder.asc $_GOREORDERBIN
rm /tmp/goreorder.asc
Please fill an issue to create a bug report.
If you want to participate, please fork the repository and propose a pull request on the "develop" branch.