在Go中为多类型数组创建通用函数

I am trying to create a generic function that can handle actions on slices in Go... for instance, append an item of any type to a slice of that same type. This is simply a generic purpose for a more complex solution, but overall the issue boils down to this example:

package main

type car struct {
    make  string
    color string
}

type submarine struct {
    name   string
    length int
}

func genericAppender(thingList interface{}, thing interface{}) []interface{} {
    return append(thingList, thing)
}

func main() {

    cars := make([]car, 0, 10)
    cars[0] = car{make: "ford", color: "red"}
    cars[1] = car{make: "chevy", color: "blue"}

    subs := make([]submarine, 0, 10)
    subs[0] = submarine{name: "sally", length: 100}
    subs[1] = submarine{name: "matilda", length: 200}

    newCar := car{make: "bmw", color: "white"}
    genericAppender(&cars, newCar)

}

The code playground is at this location

The above errors as follows:

prog.go:14: first argument to append must be slice; have interface {}

After this change you're still getting a runtime error (index out of range) however the problem is that thingList is not of type []interface{} but rather interface{} so you can't append to it. Here's an updated version of your code on playground that does a type assertion to convert it to an []interface{} in line with the append. In reality you need to do that on a separate line and check for errors.

https://play.golang.org/p/YMed0VDZrv

So to put some code here;

func genericAppender(thingList interface{}, thing interface{}) []interface{}  {
    return append(thingList.([]interface{}), thing)
}

will solve the basic problem you're facing. As noted, you still get runtime errors when indexing into the slice. Also, you could change the argument to avoid this by making it;

func genericAppender(thingList []interface{}, thing interface{}) []interface{} {
    return append(thingList, thing)
}

Here's a complete example of the second type; https://play.golang.org/p/dIuW_UG7XY

Note I also corrected the runtime error. When you use make with 3 args they are, in this order, type, length, capacity. This means the length of the array is 0 so when you try to assign to indexes 0 and 1 it was causing a panic for IndexOutoFRange. Instead I removed the middle argument so it's make([]interface{}, 10) meaning the length is initially set to 10 so you can assign to those indexes.

In the answer above if you do the following then it throws error. This is what the original question was about:

//genericAppender(subs, newCar). // Throws "cannot use subs (type []submarine) as type []interface {} in argument to genericAppender"

The trick is to convert your slice of specific type into a generic []interface{}.

func convertToGeneric(thingList interface{}) []interface{} {
        input := reflect.ValueOf(thingList)
        length := input.Len()
        out := make([]interface{},length)
        for i:=0 ;i < length; i++ {
           out[i] = input.Index(i).Interface()
        }
    return out
}

This you can call the function like this:

genericAppender(convertToGeneric(subs), newCar)

You can check modified working code here: https://play.golang.org/p/0_Zmme3c8lT