From 4367eea6bcd2229850068699afc818c48b07b5c5 Mon Sep 17 00:00:00 2001 From: qydysky Date: Fri, 11 Jun 2021 02:05:38 +0800 Subject: [PATCH] =?utf8?q?=E8=AF=AD=E6=B3=95=E7=B3=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- util/util.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 util/util.go diff --git a/util/util.go b/util/util.go new file mode 100644 index 0000000..a432f08 --- /dev/null +++ b/util/util.go @@ -0,0 +1,22 @@ +package part + +import ( + "reflect" +) + +//语法糖 + +//数组切片,重新分配内存 +// i := []int{0,1,2} +// b := SliceCut(i[:1]).([]int) +func SliceCopy(src interface{}) (des interface{}) { + srcV := reflect.ValueOf(src) + if sk := srcV.Kind();sk != reflect.Slice && sk != reflect.Array { + panic(&reflect.ValueError{Method:"reflect.Copy", Kind:sk}) + } + desV := reflect.MakeSlice(srcV.Type(),srcV.Len(),srcV.Len()) + reflect.Copy(desV, srcV) + des = desV.Interface() + return +} + -- 2.39.2