Initial array slices

drew/sql-it
Drew Bednar 11 months ago
parent 715f2d8296
commit 8ed06b4d9d

@ -0,0 +1,3 @@
module myarray
go 1.21.0

@ -0,0 +1,23 @@
package arrays
func Sum(a []int) int {
sum := 0
//aternatively
// for i := 0; i < 5; i++ {
// sum += a[i]
// }
// OR
// for i := range a {
// sum += a[i]
// }
// OR
for _, v := range a {
sum += v
}
return sum
}

@ -0,0 +1,38 @@
package arrays
import "testing"
// go test -cover will show the test coverage for our package.
func TestSum(t *testing.T) {
t.Run("sum collection of 5 of integers", func(t *testing.T) {
numbers := []int{1, 2, 3, 4, 5}
got := Sum(numbers)
expected := 15
// https://pkg.go.dev/fmt
if got != expected {
t.Errorf("got %d expected %d given, %v", got, expected, numbers)
}
})
// go test -cover will show the test coverage for our package.
// You can see that commenting out this "duplicate" function still retains 100%
// test coverage, which is how we know it's redundant.
// t.Run("sum a collection of any size", func(t *testing.T) {
// numbers := []int{1, 2, 3}
// got := Sum(numbers)
// expected := 6
// // https://pkg.go.dev/fmt
// if got != expected {
// t.Errorf("got %d expected %d given, %v", got, expected, numbers)
// }
// })
}
Loading…
Cancel
Save