dongrang2140 2018-11-09 16:42
浏览 581
已采纳

为什么在golang中的类型错误下方出现此紧急示例?

Why is this panicf-sprintf causing a type error in Golang 1.11? Go doesn't explain the reason, even if it says this is a common mistake.

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

go vet is now enforced during the build.

func panicf(s string, i ...interface{}) { panic(fmt.Sprintf(s, i)) }

The test is returning

missing ... in args forwarded to printf-like function

vet describes this as

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"

Please help explain ... in golang in this context.

Here is a functional playground: https://play.golang.org/p/DijjanQNkxK

  • 写回答

1条回答 默认 最新

  • dthh5038 2018-11-09 16:47
    关注

    panicf() accepts i as a variadic, the same as fmt.Sprintf(). Therefore you have to tell the compiler that you want each value of i to be sent to fmt.Sprintf() instead of sending the entire thing as a slice.

    So change the code to:

    func panicf(s string, i ...interface{}) { panic(fmt.Sprintf(s, i...)) }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64