Test 4 should test len(output) == 0 since either []int{} or nil would fit the criteria (nil would be more idiomatic). Failing the hidden test case for returning nil instead of []int{} would be very annoying.
Go comes with testing built-in, the use of an external testing library seems unnecessary. reflect.DeepEqual(want, got) would be enough for Tests 1-3, and len(got) == 0 for Test 4, or the negation of these to signal test failure.
Two alternate ways to write the loop:
for _, val := range l {
if n, ok := val.(int); ok {
output = append(output, n)
}
}
for _, val := range l {
switch val := val.(type) {
case int:
output = append(output, val)
}
}
Ginkgo is the testing framework used for Go on Codewars. It's rather the contrary: don't use
reflect
when the framework can do the job.In the solution,
var output []int
would be my pick. We don't need to allocate if there's nothing to do. See https://github.com/golang/go/wiki/CodeReviewComments#declaring-empty-slices.Test 4 should test
len(output) == 0
since either[]int{}
ornil
would fit the criteria (nil
would be more idiomatic). Failing the hidden test case for returningnil
instead of[]int{}
would be very annoying.Go comes with testing built-in, the use of an external testing library seems unnecessary.
reflect.DeepEqual(want, got)
would be enough for Tests 1-3, andlen(got) == 0
for Test 4, or the negation of these to signal test failure.Two alternate ways to write the loop: