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
Different ways for Integer to String Conversions
Example: Arrays of Arrays, Arrays of Slices, Slices of Arrays and Slices of Slices
Golang String Concatenation
How to use a mutex to define critical sections of code and fix race conditions?
How to include and execute HTML template?
How to wait for Goroutines to Finish Execution?
How to declare and access pointer variable?
How to concatenate two or more slices in Golang?
Golang import package inside package
How to read/write from/to file in Golang?