知识卡片

操作参考卡 · 4545.g.8.i

可变参数在函数内是切片

理解等级:中级

末尾参数写成…T后,调用者可以给零个或多个T值,函数内部按[]T访问它们。已有切片可在调用处以slice…展开。它不是任意位置参数都可变,也不免除元素类型约束;这个形式适合一组同类输入,interface{}则是另一层泛化。

典型形式(语法示例):func sum(v ...int) int;调用已有切片时写sum(values...)。

这张卡片看不懂?

已有反馈 · 0 / 10 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Go Language Foundations · 查看原书依据

      Within the variadic function, the variadic argument is a slice of the argument type. In the below example, the variadic factors parameter of the product method is of type []int and may be ranged over accordingly. func product(factors ...int) int { p := 1 for _, n := range factors { p *= n }

    参考来源

    - 位置:《Cloud Native Go》第03章《Go Language Foundations》(来源项目book-sources;源文件:extracted/Cloud Native Go (Matthew A. Titmus)/_epub-src/OEBPS/ch03.xhtml) - 结论依据:末尾参数写成...T后,调用者可以给零个或多个T值,函数内部按[]T访问它们。原文相应定义、示例或限制为此结论提供依据;补充纠错与版本边界在正文就近标明。 - 原始内容:Within the variadic function, the variadic argument is a slice of the argument type. In the below example, the variadic factors parameter of the product method is of type []int and may be ranged over accordingly. func product(factors ...int) int { p := 1 for _, n := range factors { p *= n }