Order imports into blocks
go install github.com/sabahtalateh/importblocks@latest
You may also need to modify your ~/.[bash|zsh|fish..]rc
with
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN
So modifications to take effect run
source ~/.[bash|zsh|fish..]rc
importblocks [-config ..] [-lookup ..] [-v] [path ..]
-config
relative or absolute path to config file. (see: Config File)-lookup
lookup config file name. (see: Lookup Config File)-v
verbose modepath
file or directory relative or absolute path. If ends with/...
then inner files and dirs will be formatted recursively. Ex:code/go/project1/...
,./...
. Multiple paths may be specified
NOTE: if no -config
or -lookup
passed then default config will be used which is suitable for most cases
importblocks:
- [ "!std" ]
- [ "*" ]
- [ "!mod" ]
After installation run this command on your project and see the result
importblocks ./...
Suppose you have
import (
"a/b/c"
"fmt"
"x/y/z"
"os"
"github.com/my/module/pkg2" // your module package
"math"
"github.com/my/module/pkg1" // your module package
"github.com/thirdparty/pkg1"
"golang.org/x/exp"
"a/b/c2"
)
To order imports in your project root create importblocks.yaml
file with content
importblocks:
- [ "!std" ] # standard library
- [ "github.com/thirdparty", "golang.org/x" ] # some package prefixes
- [ "*" ] # everything else
- [ "!mod" ] # your module packages
Then run program from your module dir
importblocks -config imports.yaml ./...
Imports will be grouped by blocks. Imports within block sorted alphabetically
import (
"fmt"
"os"
"math"
"github.com/thirdparty/pkg1"
"golang.org/x/exp"
"a/b/c"
"a/b/c2"
"x/y/z"
"github.com/my/module/pkg1"
"github.com/my/module/pkg2"
)
Config file is yaml
with 2-dimensional array of specifiers where specifier can be one of
!std
to mention any standard library package!mod
to mention your module package- Any string. Package prefix to mention package path starting with it
*
to mention any package path which not specified by other specifiers
Multiple config files may exist on different levels of directories hierarchy. With this feature you may have common config for different projects inside one directory and specific config for each project. Nested config content will be appended to topmost
Example
root
├──project1
│ └──imports.yaml
├──project2
│ └──imports.yaml
└──imports.yaml
# root/imports.yaml
importblocks:
- [ "!std" ]
- [ "*" ]
# root/project1/imports.yaml
importblocks:
- [ "root/project1" ]
Then
$ cd root/project1
$ importblocks -lookup imports.yaml ./...
.go
-files will be formatted with computed config root/imports.yaml
+ root/project1/imports.yaml
importblocks:
- [ "!std" ]
- [ "*" ]
- [ "root/project1" ]