zhangyachen / zhangyachen.github.io

zhangyachen's blog
274 stars 39 forks source link

golang 三个点(three dots)的用法 #137

Open zhangyachen opened 5 years ago

zhangyachen commented 5 years ago

已经忘了这是第几次查这个用法了,还是记一下吧~ ^ _ ^

在Golang中,三个点一共会用在四个地方(话说三个点的官方说法是什么?):

变长的函数参数

如果最后一个函数参数的类型的是...T,那么在调用这个函数的时候,我们可以在参数列表的最后使用若干个类型为T的参数。这里,...T在函数内部的类型实际是[]T.

func Sum(nums ...int) int {
    res := 0
    for _, n := range nums {
        res += n
    }
    return res
}

Sum(1,2,3)

调用拥有变长参数列表的函数

上面调用Sum函数时,是将变长参数分开写的。如果我们有一个slice,那么我们调用时不必将slice拆开再调用,直接在slice后跟...即可:

primes := []int{2, 3, 5, 7}
fmt.Println(Sum(primes...)) // 17

标识数组元素个数

这里,...意味着数组的元素个数:

stooges := [...]string{"Moe", "Larry", "Curly"} // len(stooges) == 3

Go命令行中的通配符

描述包文件的通配符。 在这个例子中,会单元测试当前目录和所有子目录的所有包:

go test ./...

参考资料:

朋友们可以关注下我的公众号,获得最及时的更新:

image

edocevol commented 5 years ago

And, you can use it in below case

s1 := []int{0, 1, 2, 3}
s2 := []int{4, 5, 6, 7}
s1 = append(s1, s2...) // instead of FOR
fmt.Println(s1)
renyddd commented 4 years ago

cmd := exec.Command("/proc/self/exe", append([]string{"child"}, os.Args[2:]...)...)

包含以上前两种情况,其中:

func Command(name string, arg ...string) *Cmd

代码来自:https://github.com/lizrice/containers-from-scratch/blob/master/main.go