Single function iterate a map or slice with generics

Viewed 50

Is there a way using Go's new generics to have a single for in a function be able to iterate over a loop or a slice?

This is roughly what I'm wanting to do, with a little more logic attached based on keys.

func iterateContent[K []interface{} | map[string]interface{}](items K) {
    for k, _ := range items {
        fmt.Println(k)
    }
}

This code though gives me the following error

cannot range over items (variable of type K constrained by []interface{}|map[string]interface{}) (K has no core type)


There are other questions on StackOverflow about how to iterate varying types of slices, but they're fundamentally different than varying between a slice and a map.

0 Answers
Related