我在Golang项目中做了一些修改,后来运行了make test
,它负责linting,格式化和单元测试。但是当它运行www.example.com时linter.sh,它抛出以下错误
pkg/skaffold/kubernetes/wait.go:23: File is not `goimports`-ed with -local github.com/GoogleContainerTools/skaffold (goimports)
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubectl"
这里是Code的链接。
9条答案
按热度按时间vc9ivgsu1#
仅仅执行普通的
Sort imports
可能无法工作。我认为你有goimports
linting与local-prefixes
启用,这就是为什么错误File is not 'goimports'-ed with -local ...
通常情况下,
goimports
会对导入的库进行排序,以便将标准pkg和其他库放在一个单独的组中。然而,当你启用了本地前缀时,linting需要标准的pkg,第三方的pkg,以及带有指定的本地前缀的pkg(在你的例子中是github.com/GoogleContainerTools/skaffold
,也就是你自己的项目pkg),这三种类型在单独的组中。(ref:https://github.com/golangci/golangci-lint/issues/209)这些不一定要在3组,你可以有更多的3组。只要确保以上3种类型(或2种)不在同一种类型中即可。
修复
当你运行
goimports
时,确保你运行它时带有-local
标志。我想你也可以配置你的IDE来做到这一点。在您的情况下,它应该看起来像这样:-w标志,以便将更改写回
**. (点) 表示所有文件,也可以只指定一个文件
gg0vcinb2#
我犯了同样的错误。皮棉是
golangci-lint
。我对导入进行了排序(使用GoLand),整理了依赖项(
go mod tidy
)并删除了空白(gofmt -w app.go
)。除了以下几点之外,没有任何效果:
golangci-lint run --fix
dbf7pr2w3#
在我的情况下,我必须改变这一点:
对此:
ou6hu8tu4#
当IDE和linter对导入进行排序的设置不同时,通常会发生此问题。我使用 GoLand IDE和 golangci-lint 作为linter。我更改了IDE设置,使用***goimports***代替***gofmt***,如下图所示,它为我解决了这些错误。
nkhmeac65#
goimports
有一个问题来排序这样的东西:它会像它一样生活。要使其格式化并可接受,您可以尝试此第三方来修复包导入顺序:https://github.com/incu6us/goimports-reviser
范例:
xtfmy6hx6#
我看了你的代码,很明显问题出在你的导入上。你必须对你的文件应用
goimports
命令来正确地对导入进行排序(或者如果你使用Goland,可以用IDE工具来完成)。关于Goland集成的信息:https://www.jetbrains.com/help/go/integration-with-go-tools.html
gojuced77#
只要遵循两个步骤,它就会很好地工作:
1.在本地安装
goimports
,运行如下:-去安装golang.org/x/tools/cmd/goimports@latest
1.然后,对于每个文件运行:-
goimports -w file_name.go
ufj5ltwl8#
有时候,删除(部分)注解块是唯一的解决办法。例如,这导致了我的一个文件中的问题,并且**goimports -w...**没有修复任何关于它的问题:
46scxncf9#
我在注解中添加JSON示例时也遇到了同样的问题
这导致我的go-lint失败。当我从评论消息中删除空格(//和第一个字母之间的空格)时,问题解决了。
这是一个很好的黑客删除这些linting错误。