为什么这个恐慌示例下面是 golang 中的类型错误?

sha*_*wbq 3 types go

为什么这个 panicf-sprintf在 Golang 1.11 中导致类型错误?Go 没有解释原因,即使它说这是一个常见的错误。

https://golang.org/doc/go1.11#vet

go vet 现在在构建期间强制执行。

func panicf(s string, i ...interface{}) { panic(fmt.Sprintf(s, i)) }
Run Code Online (Sandbox Code Playgroud)

考试回来了

missing ... in args forwarded to printf-like function
Run Code Online (Sandbox Code Playgroud)

vet 将此描述为

func (*ptrStringer) BadWrap(x int, args ...interface{}) string {
    return fmt.Sprint(args) // ERROR "missing ... in args forwarded to print-like function"
}

func (*ptrStringer) BadWrapf(x int, format string, args ...interface{}) string {
    return fmt.Sprintf(format, args) // ERROR "missing ... in args forwarded to printf-like function"
Run Code Online (Sandbox Code Playgroud)

在这种情况下,请...golang中帮助解释。

这是一个功能性的游乐场:https : //play.golang.org/p/DijjanQNkxK

poy*_*poy 7

panicf()接受i为可变参数,与fmt.Sprintf(). 因此,您必须告诉编译器您希望将 的每个值i发送到,fmt.Sprintf()而不是将整个内容作为切片发送。

所以把代码改成:

func panicf(s string, i ...interface{}) { panic(fmt.Sprintf(s, i...)) }
Run Code Online (Sandbox Code Playgroud)