代码拉取完成,页面将自动刷新
package gutil
import (
"gitee.com/micro-tools/wf/util/gconv"
"reflect"
)
// SliceCopy does a shallow copy of slice `data` for most commonly used slice type
// []interface{}.
func SliceCopy(data []interface{}) []interface{} {
newData := make([]interface{}, len(data))
copy(newData, data)
return newData
}
// SliceDelete deletes an element at `index` and returns the new slice.
// It does nothing if the given `index` is invalid.
func SliceDelete(data []interface{}, index int) (newSlice []interface{}) {
if index < 0 || index >= len(data) {
return data
}
// Determine array boundaries when deleting to improve deletion efficiency.
if index == 0 {
return data[1:]
} else if index == len(data)-1 {
return data[:index]
}
// If it is a non-boundary delete,
// it will involve the creation of an array,
// then the deletion is less efficient.
return append(data[:index], data[index+1:]...)
}
// SliceToMap converts slice type variable `slice` to `map[string]interface{}`.
// Note that if the length of `slice` is not an even number, it returns nil.
// Eg:
// ["K1", "v1", "K2", "v2"] => {"K1": "v1", "K2": "v2"}
// ["K1", "v1", "K2"] => nil
func SliceToMap(slice interface{}) map[string]interface{} {
var (
reflectValue = reflect.ValueOf(slice)
reflectKind = reflectValue.Kind()
)
for reflectKind == reflect.Ptr {
reflectValue = reflectValue.Elem()
reflectKind = reflectValue.Kind()
}
switch reflectKind {
case reflect.Slice, reflect.Array:
length := reflectValue.Len()
if length%2 != 0 {
return nil
}
data := make(map[string]interface{})
for i := 0; i < reflectValue.Len(); i += 2 {
data[gconv.String(reflectValue.Index(i).Interface())] = reflectValue.Index(i + 1).Interface()
}
return data
}
return nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。