From d98b521b1af1f0b8df0bd691e86b0193047daf33 Mon Sep 17 00:00:00 2001 From: Jacob <2606873+unitoftime@users.noreply.github.com> Date: Mon, 17 Feb 2025 17:11:23 -0500 Subject: [PATCH] Add safe sliceget function --- ds/slice.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ds/slice.go b/ds/slice.go index e1a3103..46e994c 100644 --- a/ds/slice.go +++ b/ds/slice.go @@ -13,3 +13,12 @@ func GrowAdd[K any](slice []K, idx int, val K) []K { slice[idx] = val return slice } + +// Safely gets and returns a value from a slice, and a boolen to indicate boundscheck +func SafeGet[K any](slice []K, idx int) (K, bool) { + if idx < 0 || idx >= len(slice) { + var k K + return k, false + } + return slice[idx], true +}