dourunlao1642 2015-02-14 04:29
浏览 57
已采纳

Go中“…Type”是什么意思?

This code is in builti.go:

// The append built-in function appends elements to the end of a slice. If
// it has sufficient capacity, the destination is resliced to accommodate the
// new elements. If it does not, a new underlying array will be allocated.
// Append returns the updated slice. It is therefore necessary to store the
// result of append, often in the variable holding the slice itself:
//  slice = append(slice, elem1, elem2)
//  slice = append(slice, anotherSlice...)
// As a special case, it is legal to append a string to a byte slice, like this:
//  slice = append([]byte("hello "), "world"...)
func append(slice []Type, elems ...Type) []Type

The last line made me feel very confused. I do not know the meaning of ...Type .

These are other codes:

package main

import "fmt"

func main() {
   s := []int{1,2,3,4,5}
   s1 := s[:2]
   s2 := s[2:]
   s3 := append(s1, s2...)
   fmt.Println(s1, s2, s3)
}

The result is

[1 2] [3 4 5] [1 2 3 4 5]

I guess the function of ... is to pick all elements from elems, but I haven't found an official explanation. What is it?

  • 写回答

1条回答 默认 最新

  • duanmi8349 2015-02-14 04:47
    关注

    The code in builtin.go serves as documentation. The code is not compiled.

    The ... specifies that the final parameter of the function is variadic. Variadic parameters are documented in the Go Language specification.

    The Type part is a stand-in for any Go type.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗