gommon icon indicating copy to clipboard operation
gommon copied to clipboard

[go] goimport that check and replace specific import

Open at15 opened this issue 5 years ago • 7 comments

Now I use goimport instead of gofmt when formatting code because it checks import and do the sorting. However I have some new use cases:

  • replace specific import because they were added by IDE when there are imports with similar name. Especially when your dependency depends on its own fork of your other dependency, e.g. assert.
  • group import into multiple groups, e.g. protobuf, third party dependency, packages inside the project
  • enforce import alias, e.g. protobuf packages should be named to xxxpb

Should put it under gommon fmt or gommon gofmt

at15 avatar Jan 01 '20 22:01 at15

I should be part of #75

at15 avatar Feb 28 '20 23:02 at15

Found one linter from m3db https://github.com/m3db/build-tools/tree/master/linters/importorder

at15 avatar Feb 29 '20 19:02 at15

Also there is one checking if specific import is allowed https://github.com/fatih/faillint

at15 avatar Mar 06 '20 19:03 at15

Source of goimports https://github.com/golang/tools/blob/master/cmd/goimports/goimports.go

at15 avatar Mar 08 '20 09:03 at15

https://pkg.go.dev/golang.org/x/tools/go/ast/astutil?tab=doc#AddImport astutil contains add and remove imports, looks pretty good ... not sure if we can do something similar to goimports using that

at15 avatar Mar 08 '20 21:03 at15

https://github.com/cockroachdb/crlfmt/blob/master/main.go#L238 found one from cockroachdb, crlfmt

at15 avatar Mar 08 '20 23:03 at15

https://github.com/OpenPeeDeeP/depguard found from golint ci

at15 avatar Mar 09 '20 01:03 at15