diff --git a/clone.go b/clone.go index 6723ec0..788ed16 100644 --- a/clone.go +++ b/clone.go @@ -52,7 +52,7 @@ func reflectClone(dst, src reflect.Value, keepPrivateFields, checkClone, noMake } if !noMake { - dst.Set(reflect.MakeSlice(styp, src.Len(), src.Cap())) + dst.Set(reflect.MakeSlice(styp, 0, src.Cap())) } fallthrough @@ -62,7 +62,10 @@ func reflectClone(dst, src reflect.Value, keepPrivateFields, checkClone, noMake simple := isSimple(styp.Elem().Kind()) hasClone := isPtr && isCloner(styp.Elem().Elem()) != math.MaxInt for i := 0; i < src.Len(); i++ { - dst, src := dst.Index(i), src.Index(i) + src := src.Index(i) + if src.Kind() == reflect.Invalid { + continue + } if simple { dst.Set(src) continue @@ -75,7 +78,7 @@ func reflectClone(dst, src reflect.Value, keepPrivateFields, checkClone, noMake src = src.Elem() ndst := reflect.New(src.Type()) reflectClone(ndst.Elem(), src, keepPrivateFields, hasClone, false) - dst.Set(ndst) + dst = reflect.Append(dst, ndst) continue } @@ -86,10 +89,12 @@ func reflectClone(dst, src reflect.Value, keepPrivateFields, checkClone, noMake src = src.Elem() if isSimple(src.Kind()) { - dst.Set(src) + dst = reflect.Append(dst, src) continue } - dst.Set(maybeCopy(src, keepPrivateFields)) + if src.Kind() != reflect.Invalid { + dst = reflect.Append(dst, maybeCopy(src, keepPrivateFields)) + } } case reflect.Map: @@ -184,7 +189,7 @@ func isSimple(k reflect.Kind) bool { } func maybeCopy(src reflect.Value, copyPrivate bool) reflect.Value { - if src.IsZero() { + if src.Kind() == reflect.Invalid || src.IsZero() { return src }