Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Variadic function
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===In Go=== Variadic functions in [[Go (programming language)|Go]] can be called with any number of trailing arguments.<ref>{{Cite web|url=https://gobyexample.com/variadic-functions|title=Go by Example: Variadic Functions}}</ref> {{code|fmt.Println}} is a common variadic function; it uses an empty interface as a catch-all type. <syntaxhighlight lang="go"> package main import "fmt" // This variadic function takes an arbitrary number of ints as arguments. func sum(nums ...int) { fmt.Print("The sum of ", nums) // Also a variadic function. total := 0 for _, num := range nums { total += num } fmt.Println(" is", total) // Also a variadic function. } func main() { // Variadic functions can be called in the usual way with individual // arguments. sum(1, 2) // "The sum of [1 2] is 3" sum(1, 2, 3) // "The sum of [1 2 3] is 6" // If you already have multiple args in a slice, apply them to a variadic // function using func(slice...) like this. nums := []int{1, 2, 3, 4} sum(nums...) // "The sum of [1 2 3 4] is 10" } </syntaxhighlight> '''Output:''' <pre> The sum of [1 2] is 3 The sum of [1 2 3] is 6 The sum of [1 2 3 4] is 10 </pre>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)