From c211d626203b9355fb72e6796faa7aba8728ec0c Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Thu, 13 Jul 2023 11:26:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20slice=20=E5=8C=85=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=95=B0=E7=BB=84=E7=9A=84=E9=83=A8=E5=88=86?= =?UTF-8?q?=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/slice/slice.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/utils/slice/slice.go b/utils/slice/slice.go index 7db1a7b..a9d1d12 100644 --- a/utils/slice/slice.go +++ b/utils/slice/slice.go @@ -129,3 +129,30 @@ func Merge[V any](slices ...[]V) []V { } return slice } + +// GetStartPart 获取数组的前 n 个元素 +func GetStartPart[V any](slice []V, n int) []V { + if n > len(slice) { + n = len(slice) + } + return slice[:n] +} + +// GetEndPart 获取数组的后 n 个元素 +func GetEndPart[V any](slice []V, n int) []V { + if n > len(slice) { + n = len(slice) + } + return slice[len(slice)-n:] +} + +// GetPart 获取数组的部分元素 +func GetPart[V any](slice []V, start, end int) []V { + if start < 0 { + start = 0 + } + if end > len(slice) { + end = len(slice) + } + return slice[start:end] +}