Flatten_array tests for Go

I think there is a problem with the tests for flatten_array.go. I am getting errors like this:

flatten_array_test.go:19: Flatten() = &, want:

Line 19 of the test does use a pointer (&actual), unlike the other tests (which use actual). I can’t see any reason why, and my program is not returning a pointer.

Apologies if I am missing something!

Moved to the Go category.

You might want to wrap the error in a codeblock. It currently shows unicode boxes, which I’m pretty sure aren’t part of the exercise! Sharing your solution code (in a codeblock) would also help a lot!

1 Like

Sorry, hopefully this looks better.

All the errors look like this; the only problem that I can see is that it seems to use a pointer to refer to the result on line 19 of the test:

flatten_array_test.go:19: Flatten([1 <nil> <nil> 4]) = &[1 4], want: [1 4]

Here is my solution:

package flatten

func Flatten(nested interface{}) []int {
	returnList := []int{}
    switch v := nested.(type) {
    case int:
        returnList = append(returnList, v)
    case []any:
        returnList = append(returnList, UnpackSlice(v)...)
    }    	
    
    return returnList
}

func UnpackSlice(slice interface{}) []int {
    returnList := []int{}
    switch v := slice.(type) {
        case int:
            returnList = append(returnList, v)
        case []any:
        	if len(v) > 0 {
                for i := 0; i < len(v); i++ {
            		returnList = append(returnList, UnpackSlice(v[i])...)
                }
            }
        }
    return returnList
}

The exercise starts you off with the following stub:

package flatten

func Flatten(nested interface{}) []interface{} {
	panic("Please implement the Flatten function")
}

It looks like you changed the function signature! I would suggest trying to fix that first :slight_smile:

That fixed it! I still don’t understand why the test needs to use a pointer, or why a blank interface is needed given that the tests all use int slices. Maybe one day I will :slight_smile:

I’m not sure what’s going on there myself, either ;)

There’s no reason the function should stop working if/when a test case decides to mix things up!

1 Like