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
Golang Convert String into Snake Case
Functions mess (recursive anonymous function) in Golang
How to convert Go struct to JSON?
Replace first occurrence of string using Regexp
Example Function that takes an interface type as value and pointer?
How to kill execution of goroutine?
Runtime package variables
Convert Int data type to Int16 Int32 Int64
How to Convert string to float type in Go?