Skip to content

Commit 433b6a8

Browse files
committed
feat(container): add 'EmptySliceIfNil' function
1 parent 4e9b111 commit 433b6a8

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

container/slice.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,12 @@ func InSlice[T comparable](v T, items []T) bool {
2828
}
2929
return false
3030
}
31+
32+
// EmptySliceIfNil checks if the given slice is nil. If it is, it returns an empty slice of the same type.
33+
// Otherwise, it returns the original slice.
34+
func EmptySliceIfNil[T any](src []T) (dst []T) {
35+
if src == nil {
36+
return make([]T, 0)
37+
}
38+
return src
39+
}

0 commit comments

Comments
 (0)