diff --git a/helper/func.go b/helper/func.go index 75ec66d..1eba333 100644 --- a/helper/func.go +++ b/helper/func.go @@ -309,6 +309,14 @@ func Max[T IntNumber | ~float64 | ~float32](a ...T) T { return max } +func Sum[T IntNumber | ~float64 | ~float32](a ...T) T { + s := T(0) + for _, t := range a { + s += t + } + return s +} + func SliceChunk[T any](arr []T, size int) [][]T { var r [][]T i := 0 diff --git a/helper/func_test.go b/helper/func_test.go index 6414789..8b102ba 100644 --- a/helper/func_test.go +++ b/helper/func_test.go @@ -744,3 +744,27 @@ func TestSliceChunk(t *testing.T) { }) } } + +func TestSum(t *testing.T) { + type args struct { + a []int + } + tests := []struct { + name string + args args + want int + }{ + { + name: "t1", + args: args{a: RangeSlice(1, 10, 1)}, + want: 55, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := Sum(tt.args.a...); got != tt.want { + t.Errorf("Sum() = %v, want %v", got, tt.want) + } + }) + } +}