![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)
!['logo'](https://github.com/incu6us/goimports-reviser/raw/v2.1.1/images/reviser-muscot_200.png)
Tool for Golang to sort goimports by 3-4 groups: std, general, local(which is optional) and project dependencies.
Also, formatting for your code will be prepared(so, you don't need to use gofmt
or goimports
separately).
Use additional options -rm-unused
to remove unused imports and -set-alias
to rewrite import aliases for versioned packages or for packages with additional prefix/suffix(example: opentracing "github.com/opentracing/opentracing-go"
).
-local
- will create group for local imports. Values should be comma-separated.
The last two options (-rm-unused
& -set-alias
) will work only for projects using Go Modules.
Install
With Brew
brew tap incu6us/homebrew-tap
brew install incu6us/homebrew-tap/goimports-reviser
With Snap
snap install goimports-reviser
How To Use
!['How To'](https://github.com/incu6us/goimports-reviser/raw/v2.1.1/images/howto.gif)
Before usage:
package testdata
import (
"log"
"github.com/incu6us/goimports-reviser/testdata/innderpkg"
"bytes"
"github.com/pkg/errors"
)
After usage:
package testdata
import (
"bytes"
"log"
"github.com/pkg/errors"
"github.com/incu6us/goimports-reviser/testdata/innderpkg"
)
Comments(not Docs) for imports is acceptable. Example:
package testdata
import (
"fmt" // comments to the package here
)
Example with -local
-option
Before usage:
package testdata // goimports-reviser/testdata
import (
"fmt" //fmt package
"github.com/pkg/errors" //custom package
"github.com/incu6us/goimports-reviser/pkg" // this is a local package which is not a part of the project
"goimports-reviser/pkg"
)
After usage:
package testdata // goimports-reviser/testdata
import (
"fmt" // fmt package
"github.com/pkg/errors" // custom package
"github.com/incu6us/goimports-reviser/pkg" // this is a local package which is not a part of the project
"goimports-reviser/pkg"
)
Options:
Usage of goimports-reviser:
-file-path string
File path to fix imports(ex.: ./reviser/reviser.go). Required parameter.
-project-name string
Your project name(ex.: github.com/incu6us/goimports-reviser). Required parameter.
-local string
Local package prefixes which will be placed after 3rd-party group(if defined). Values should be comma-separated. Optional parameters.
-rm-unused
Remove unused imports. Optional parameter.
-set-alias
Set alias for versioned package names, like 'github.com/go-pg/pg/v9'. In this case import will be set as 'pg "github.com/go-pg/pg/v9"'. Optional parameter.
Examples:
Cmd
goimports-reviser -project-name github.com/incu6us/goimports-reviser -file-path ./reviser/reviser.go -rm-unused -set-alias
![example](https://github.com/incu6us/goimports-reviser/raw/v2.1.1/images/image.png)