go中三个点用法

go命令中三个点含义

An import path is a pattern if it includes one or more "..." wildcards, each of which can match any string, including the empty string and strings containing slashes. Such a pattern expands to all package directories found in the GOPATH trees with names matching the patterns. As a special case, x/... matches x as well as x's subdirectories. For example, net/... expands to net and packages in its subdirectories.

参考:https://blog.csdn.net/dongfengkuayue/article/details/52769322

golang中的三个点"…"的用法

‘…’ 其实是go的一种语法糖。 

它的第一个用法主要是用于函数有多个不定参数的情况,可以接收多个不确定数量的参数。  

第二个用法是slice可以被打散进行传递。

示例如下:

func test1(args ...string) {        //可以接受任意个string参数
    for _, v:= range args{
        fmt.Println(v)
    }
}

func main(){
var strss= []string{
        "qwr",
        "234",
        "yui",
        "cvbc",
    }
    test1(strss...)      //切片被打散传入
}

结果:

qwr
234
yui
cvbc

其中strss切片内部的元素数量可以是任意个,test1函数都能够接受。

第二个例子: 

var strss= []string{
        "qwr",
        "234",
        "yui",

}
var strss2= []string{
        "qqq",
        "aaa",
        "zzz",
        "zzz",
}
strss=append(strss,strss2...)      //strss2的元素被打散一个个append进strss
fmt.Println(strss)

结果:

[qwr 234 yui qqq aaa zzz zzz]

参考:https://blog.csdn.net/jeffrey11223/article/details/79166724

猜你喜欢

转载自www.cnblogs.com/embedded-linux/p/10961368.html