lundi 19 juillet 2021

Merge slice of structs, dynamically

My function get takes a slice of arbitrary structs (always a slice) as the results parameter.

I want to create another slice data of the same structs, populate data, and then merge data with results.

Here's a partial implementation:

type Foo struct {
    name string
}

func get(results interface{}) {
    data = reflect.New(reflect.TypeOf(results)).Interface()
    
    // get some dataJSON string and unmarshal it
    err := json.Unmarshal([]byte(dataJSON), data)
    
    // how to append data to results?
}

func main() {
    results := []Foo{}
    get(results)
}

How can I merge data and results?





Aucun commentaire:

Enregistrer un commentaire