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

为什么在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 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化