dongzongzi0379 2017-11-10 14:01
浏览 28
已采纳

如何读取传递给采用某种可变参数的函数的所有参数? [重复]

This question already has an answer here:

func foo(x... int) {

//Do something with the arguments.

}

Function foo takes any number of arguments of a particular type. How do I read those arguments inside the function?

I can do so when a slice of int is passed to the function foo but not if arguments are not passed as a slice of int.

</div>
  • 写回答

1条回答 默认 最新

  • dpzo13732 2017-11-10 14:03
    关注

    I can do so when a slice of int is passed to the function foo but not if arguments are not passed as a slice of int.

    You can use "x" as a slice in both cases. The following should work regardless of the way you call the function:

    for i, v := range x {
        // ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失