Zip
Creates a collection of elements, grouped based on the position in the original collections.
- Use
rangeto iterate over theparams,reflect.ValueOf()andValue.Len()to find the longest collection. - Use
make()to create a 2Dinterface{}slice of length equal to the longest collection. - Use
make()to create a slice for each element in the result,rangeto iterate overparams. - Use
reflect.ValueOf(),Value.Len(),append(),Value.Index()andValue.Interface()to add values to the result.
import "reflect"
func Zip(params ...interface{}) [][]interface{} {
l := 0
for i := range params {
arr := reflect.ValueOf(params[i])
if l < arr.Len() {
l = arr.Len()
}
}
r := make([][]interface{}, l)
for i := 0; i < l; i++ {
r[i] = make([]interface{}, 0)
for j := range params {
v := reflect.ValueOf(params[j])
if v.Len() > i {
r[i] = append(r[i], v.Index(i).Interface())
}
}
}
return r
}