Normal function parameter with variadic function parameter
Example
package main
import "fmt"
func main() {
fmt.Println(calculation("Rectangle", 20, 30))
fmt.Println(calculation("Square", 20))
}
func calculation(str string, y ...int) int {
area := 1
for _, val := range y {
if str == "Rectangle" {
area *= val
} else if str == "Square" {
area = val * val
}
}
return area
}
Output
600
400
Most Helpful This Week
Select single argument from all arguments of variadic function
Golang Convert String into Snake Case
Subtract N number of Year, Month, Day, Hour, Minute, Second, Millisecond, Microsecond and Nanosecond to current date-time.
How to find the type of the variable by different ways in Golang?
Pass different types of arguments in variadic function
Golang Read Write Create and Delete text file