A variadic function is a function that accepts an unlimited number of parameters. Generally, they are expressed in the following form
func hello(friends ...string) {
// ...
}
It is easy to identify from three points...
.
We can callhello()
like this:
hello("tony", "mark", "mary", "joe")
or:
hello("tony")
Or use an unlimited number of strings.
Example from the standard library:append()
append
Is a variable function, defined as
func append(slice []Type, elems ...Type) []Type
A common usage is when removing items from a locationi
From the arraya
.
a = append(a[:i], a[i+1:]...)
This line creates a new slice by removing the item at the positioni
ina
, The method is to combine items from 0 to i (not included) and from i + 1 to the end.
What is the purpose...
?
append
Accept slice as the first parameter, and accept an unlimited number of parameters, all parameters have typesAssignableThe type of its element.
writing
a = append(a[:i], a[i+1:]...)
Equivalent to writing
a = append(a[:i], a[i+1], a[i+2], a[i+3], a[i+4]) //and so on, until the end of the slice.
usea[i+1:]...
Basically a shorthand syntax, as described in the Go specificationhttps://golang.org/ref/spec#Passing_arguments_to_..._parameters:
If f is a variable parameter whose final parameter p is of type...T, then the type of p within f is equivalent to the type [] T. If f is called without the actual parameter of p, the value passed to p is nil. Otherwise, the passed value is a new slice of type [] T, with a new base array, and its continuous elements are actual parameters, all of which must be assignable to T
More tutorials:
- Use NGINX reverse proxy service Go service
- Copy structure in Go
- Basics of Go web server
- Sorting map types in Go
- In a nutshell
- Go to label description
- Start date and time format
- Use Go for JSON processing
- Variadic function
- Cheat sheet
- Go to the empty interface description
- Use VS Code and Delve to debug Go
- Named Go return parameter
- Generate random numbers and strings in Go
- File system structure of Go project
- Binary search algorithm in Go
- Use command line flags in Go
- GOPATH explained
- Use Go to build a command line application: lolcat
- Use Go to build CLI commands: Cowsay
- Use shell and tube in Go
- Go CLI Tutorial: Fortune Clone
- Use Go to list files in a folder
- Use Go to get a list of repositories from GitHub
- Go, append a short string to the file
- Go, convert the string to byte slices
- Use Go to visualize your local Git contributions
- Getting started with Go CPU and memory analysis
- Solve the "Does not support index" error in the Go program
- Measuring the execution time in the Go program
- Use Go to build a web crawler to detect duplicate titles
- Best Practice: Pointer or Value Receiver?
- Best practice: Should you use methods or functions?
- Go data structure: set
- Go to the map cheat sheet
- Generating the implementation of generic types in Go
- Go data structure: dictionary
- Go data structure: hash table
- Implement event listeners in "through the channel"
- Go data structure: stack
- Go data structure: queue
- Go data structure: binary search tree
- Go data structure: graphics
- Go data structure: linked list
- A complete guide to Go data structures
- Compare Go value
- Is Go object-oriented?
- Use SQL database in Go
- Use environment variables in Go
- Last tutorial: REST API supported by PostgreSQL
- Enable CORS on the Go web server
- Deploy Go application in Docker container
- Why Go is a powerful language to learn as a PHP developer
- Go and delete the io.Reader.ReadString newline
- To start, how to watch the changes and rebuild the program
- To count the months since the date
- Access HTTP POST parameters in Go