From 5f11ccf9f6bd2c885f74ef869e132549b0aec848 Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Sat, 4 Nov 2023 13:07:40 -0400 Subject: [PATCH] created variadic sum function --- headFirstCodeMagnets/variadic.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 headFirstCodeMagnets/variadic.go diff --git a/headFirstCodeMagnets/variadic.go b/headFirstCodeMagnets/variadic.go new file mode 100644 index 0000000..3ad8f88 --- /dev/null +++ b/headFirstCodeMagnets/variadic.go @@ -0,0 +1,18 @@ +package main + +import "fmt" + +func sum(numbers ...int) int { + var sum int = 0 + + for _, number := range numbers { + sum += number + } + + return sum +} + +func main() { + fmt.Println(sum(4, 1, 9, 2)) + fmt.Println(sum(7)) +}