SoFunction
Updated on 2025-03-05

Detailed explanation of the usage example of the three points '...' in golang

‘…’ is actually a kind of syntactic sugar for go.
Its first usage is mainly used when a function has multiple uncertain parameters, and it can accept multiple uncertain number of parameters.
The second usage is that slice can be broken and passed.

Here is an example:

func test1(args ...string) { // Can accept any string parameters  for _, v:= range args{
    (v)
  }
}

func main(){
var strss= []string{
    "qwr",
    "234",
    "yui",
    "cvbc",
  }
  test1(strss...) //The slices are broken and passed in}

result:

qwr
234
yui
cvbc

The number of elements inside the strss slice can be any one, and the test1 function can accept it.

The second example:

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

  }
  var strss2= []string{
    "qqq",
    "aaa",
    "zzz",
    "zzz",
  }
strss=append(strss,strss2...) //The elements of strss2 are broken up one by one append into strss(strss)

result:

[qwr 234 yui qqq aaa zzz zzz]

If there is no '...', facing the above situation, the number of code will undoubtedly increase. With '...', does it feel much more concise?

This is the end of this article about the detailed explanation of the usage examples of the three points in golang. This is the end. For more relevant content on the usage of the three points in golang, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!