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
Replace any non-alphanumeric character sequences with a dash using Regex
How to get the current date and time with timestamp in local and other timezones ?
How to read names of all files and folders in current directory?
Example of Pointers with Struct
How to collect information about garbage collection?
How to set timeout for http.Get() requests in Golang?