gofimports/README.md

65 lines
1.5 KiB
Markdown
Raw Normal View History

2022-11-24 15:25:13 +00:00
# gofimports
2022-11-24 18:29:27 +00:00
2022-11-27 17:36:32 +00:00
Group Go imports with my opinionated preferences.
First is standard.
Then third party, then company if exist.
The last is local.
Also main selling point of this is group imports not sort imports.
So please run `gofumpt` or `gofmt` after running this tool.
Under the hood, this tool get all imports, then group them into 4 groups (std, third party, company, local).
Remember, no sort here.
Then insert empty import (empty path) between each group to get final imports
Then update Go ast decls import with final imports.
There is side effect of course, because we do not create empty line but we add empty import, so there is trailing space in that line (Go indent that empty impoty).
That why I suggest you need to re-format after.
2022-11-27 17:36:32 +00:00
## Install
With Go version `>= 1.16`:
```sh
go install github.com/haunt98/gofimports/cmd/gofimports@latest
```
## Usage
```sh
# Format ./internal
# with print impacted file (-l),
# write to file (-w),
# print diff (-d)
# company is github.com/make-go-great
gofimports -l -company github.com/make-go-great -w -d ./internal
```
2022-11-24 18:29:27 +00:00
Example result:
```go
import (
"fmt"
"github.com/urfave/cli/v2"
2022-11-27 18:23:04 +00:00
"github.com/pkg/diff"
"github.com/make-go-great/color-go"
"github.com/haunt98/gofimports/internal/imports"
)
```
2022-11-26 18:23:06 +00:00
## Roadmap
- [ ] Diff with color
- [ ] Add profiling
2022-11-27 17:36:32 +00:00
- [ ] Improve performance
2022-11-26 18:23:06 +00:00
2022-11-24 18:29:27 +00:00
## Thanks
- [goimports](https://pkg.go.dev/golang.org/x/tools/cmd/goimports)
- [mvdan/gofumpt](https://github.com/mvdan/gofumpt)
- [incu6us/goimports-reviser](https://github.com/incu6us/goimports-reviser)