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

为什么在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...)) }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 adb连接不到手机是怎么回事?
  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联
  • ¥15 VB.NET操作免驱摄像头
  • ¥15 笔记本上移动热点开关状态查询
  • ¥85 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目