调用 fmt.Printf 等字符串格式化函数似乎是 Go 编译器的弱点。我最终遇到了很多错误(重构后使用了错误的格式化程序,忘记包含所有参数),这些错误只会在运行时显示出来。因此,每次写其中一个时,我最终都不得不眯着眼睛。
我今天做了一些研究并发现go tool vet,它似乎适用于 fmt.Printf,但它不会捕获 errors.Errorf 中的错误(见下文)。
import "github.com/pkg/errors"
func ReturnError(s string, i int) error {
// Swap %d and %s, and forget to include the second argument
return errors.Errorf("invalid arguments %d and %s", s)
}
是否有类似的东西go tool vet可以捕获 errors.Errorf() 中的字符串格式错误?另外,根据我自己的理解,为什么这是一个如此困难的问题?对于 Go 编译器来说,捕捉字符串格式化类型错误似乎并不比任何其他类型的错误更难。
米琪卡哇伊
相关分类