Home >Backend Development >Golang >What does... mean in golang?
What does... mean in golang?
In golang, the three dots "..." are actually a kind of syntactic sugar (sugar-coated syntax) for go.
Syntactic sugar (Syntactic sugar), also translated as sugar-coated syntax, is a term invented by British computer scientist Peter J. Landin. It refers to a certain feature added to a computer language. A syntax that has no impact on the functionality of the language, but is more convenient for programmers to use. Generally speaking, using syntax sugar can increase the readability of the program, thereby reducing the chance of program code errors.
Usage of "...":
● The first usage is mainly used when the function has multiple indefinite parameters, expressed as Variable parameters can accept any number of parameters of the same type.
● The second usage is that slices can be broken up and transferred.
The following is an example:
func test1(args ...string) { //可以接受任意个string参数 for _, v:= range args{ fmt.Println(v) } } func main(){ var strss= []string{ "qwr", "234", "yui", "cvbc", } test1(strss...) //切片被打散传入 }
Result:
qwr 234 yui cvbc
The number of elements inside the strss slice can be any number, and the test1 function can accept it .
Second example:
var strss= []string{ "qwr", "234", "yui", } var strss2= []string{ "qqq", "aaa", "zzz", "zzz", } strss=append(strss,strss2...) //strss2的元素被打散一个个append进strss fmt.Println(strss)
Result:
[qwr 234 yui qqq aaa zzz zzz]
If there is no "...", facing the above situation, the amount of code will undoubtedly increase , with '...', doesn't it feel a lot simpler?
Recommended learning: go video tutorial
The above is the detailed content of What does... mean in golang?. For more information, please follow other related articles on the PHP Chinese website!