mirror of
https://gitee.com/johng/gf.git
synced 2024-12-03 04:37:49 +08:00
34 lines
1.1 KiB
Go
34 lines
1.1 KiB
Go
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
|
//
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
// If a copy of the MIT was not distributed with this file,
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
package gutil
|
|
|
|
// 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:]...)
|
|
}
|