Variadic Function
func Example(a, b, c, ....d) {}Contoh code
package main
import (
"fmt"
)
func main() {
total := sumAll(10, 20, 30, 40, 50, 60, 70)
fmt.Println(total)
numbers := []int{10, 20, 30, 40, 50, 60, 70}
fmt.Println(sumAll(numbers...))
// numbers := [7]int{10, 20, 30, 40, 50, 60, 70} -> can not use array
// fmt.Println(sumAll(numbers...))
}
// input harus berupa slice
func sumAll(numbers ...int) int {
total := 0
for _, value := range numbers {
total += value
}
return total
}Last updated