Last active
April 16, 2025 19:33
-
-
Save stnc/c346792f4324eef5ad8b1b1d48bf000d to your computer and use it in GitHub Desktop.
How to use Ellipsis (…) in Golang? - using a variadic function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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