Home > Backend Development > Golang > What does... mean in golang?

What does... mean in golang?

青灯夜游
Release: 2019-12-23 14:19:15
Original
10434 people have browsed it

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...) //切片被打散传入
}
Copy after login

Result:

qwr
234
yui
cvbc
Copy after login

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)
Copy after login

Result:

[qwr 234 yui qqq aaa zzz zzz]
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template