Skip to content

Instantly share code, notes, and snippets.

@stnc
Last active April 16, 2025 19:33
Show Gist options
  • Save stnc/c346792f4324eef5ad8b1b1d48bf000d to your computer and use it in GitHub Desktop.
Save stnc/c346792f4324eef5ad8b1b1d48bf000d to your computer and use it in GitHub Desktop.
How to use Ellipsis (…) in Golang? - using a variadic function
package main
import (
"fmt"
)
// using a variadic function
func find(num int, nums ...int) {
fmt.Printf("type of nums is %T\n", nums)
found := false
for i, v := range nums {
if v == num {
fmt.Println(num, "found at index", i, "in", nums)
found = true
}
}
if !found {
fmt.Println(num, "not found in ", nums)
}
fmt.Printf("\n")
}
func main() {
// calling the function with
// variable number of arguments
find(89, 89, 90, 95)
find(45, 56, 67, 45, 90, 109)
find(78, 38, 56, 98)
find(87)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment