如何防止 gofmt 添加 `//go:build test`?

huangapple go评论66阅读模式
英文:

How do I prevent gofmt adding `//go:build test`

问题

在Go 1.17中,gofmt会自动更改我的文件:

//go:build test
// +build test

我已经卸载了Go 1.17并重新安装了1.16,但问题仍然存在。我该如何停止这个行为?

我应该说明,我不能使用这些标签,因为我们正在使用的代码生成工具(我无法更改)将其视为错误并导致构建失败。

英文:

In Go 1.17, gofmt automatically changes my files:

//go:build test
// +build test

I've uninstall Go 1.17 and re-installed 1.16, but the problem continues. How do I stop it?

I should say that I cannot use these tags, because the codegen we are using (which I cannot change) considers them an error and fails builds.

答案1

得分: 1

gofmt只会在检测到旧格式时添加新格式;如果只看到新格式,它不会添加旧格式。

因此,如果你知道你正在使用 go 1.17(或更高版本),你可以删除现在已经过时的构建约束行:

// +build test

并完全迁移到新的构建约束格式:

//go:build test

这样你的源代码中只会有一行构建约束。

英文:

gofmt will only add the new format if it detects the old format; if it only sees the new format - it will not add the old format.

So, if you know you are building using go 1.17 (or later), you can drop the now legacy build constraint line:

// +build test

and migrate fully to the new build constraint format:

//go:build test

to just have a single build constraint line in your source.

答案2

得分: 0

解决方案是降级到Golang 1.16,但请确保工具链的链接已正确更改回来。

英文:

The solution is to downgrade to Golang 1.16, but check that the links to tooling have been correctly changed back.

huangapple
  • 本文由 发表于 2021年9月14日 23:25:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/69180548.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定