[Go] How to use "... (3 periods)"

1.First of all

I wrote an article in the hope that it will be of benefit to those who are starting Go. When I first saw "..." in the source code, there aren't many information in Japanese even if I googled it, so I will summarize how to use "..." that I often use in business. I thought about it.

1-1. What happens if you read this article

Understand how to use "..." and the points

1-2. Target readers

People who don't understand Go's "..."

2. How to use "..."

2-1. When you want to make the function parameters variable length

Function parameters can be variable length. For example, if you have a function that wants to receive multiple ints in a variable way, you can write: The point is to write ... before the type, like ** ... int **.

//WriteInt is a function that receives and outputs a variable int
func WriteInt(nums ...int) {
	for _, v := range nums {
		fmt.Println(v)
	}
}

When executed, the result is as follows

func main() {
	WriteInt(1, 2, 3)
}

//Execution result
1
2
3

Go Playgroud

2-2. Pass all values to the variadic function

You can pass the variable-length argument function explained in the previous chapter at once by using "...". The point is to write ... after the value you want to pass, such as ** s ... **.

func main() {
	s := []int{1, 2, 3}
	
	//The value that s has...All passed to WriteInt using
	WriteInt(s...)
}

func WriteInt(nums ...int) {
	for _, v := range nums {
		fmt.Println(v)
	}
}

Go Playgroud

It is also useful when appending a slice.

func main() {
	s1 := []int{1, 2, 3}
	s2 := []int{4, 5, 6}
	s3 := append(s1, s2...)
	fmt.Println(s3)
}

//Execution result
[1 2 3 4 5 6]

reference

Official Document Passing_arguments_to ... parameters Official document append 3 dots in 4 places Expand and pass slices to variadic functions with Go ★ Ultimate Guide to Go Variadic Functions

Recommended Posts

[Go] How to use "... (3 periods)"
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use image-match
How to use Pandas 2
How to use Virtualenv
How to use pytest_report_header
How to use Bio.Phylo
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to use IPython
How to use virtualenv
How to use Matplotlib
How to use iptables
How to use numpy
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use Pyenv
How to use list []
How to use python-kabusapi
How to use OptParse
How to use return
How to use dotenv
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
How to use Qt Designer
How to use search sorted
[gensim] How to use Doc2Vec
python3: How to use bottle (2)
Understand how to use django-filter
[Python] How to use list 1
How to use FastAPI ③ OpenAPI
How to use IPython Notebook
How to use Pandas Rolling
[Note] How to use virtualenv
Python: How to use pydub
[Python] How to use checkio
How to define Go variables
How to use Django's GeoIp2
[Python] How to use input ()
How to use the decorator
[Introduction] How to use open3d
How to use Python lambda
How to use Jupyter Notebook
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Google Colaboratory
How to use Python bytes
How to use cron (personal memo)
Python: How to use async with
How to use the zip function
How to use the optparse module
How to use SWIG from waf