-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
kyle.brown
committed
Dec 6, 2024
1 parent
598e864
commit ec2c34b
Showing
1 changed file
with
10 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
package helper | ||
|
||
func SafeAccess(i, j int, data [][]byte) byte { | ||
func SafeAccess[T any](i, j int, data [][]T) T { | ||
res, _ := SafeAccessOption(i, j, data) | ||
return res | ||
} | ||
|
||
func SafeAccessOption[T any](i, j int, data [][]T) (T, bool) { | ||
var zeroValue T | ||
if i < 0 || i >= len(data) { | ||
return 0 | ||
return zeroValue, false | ||
} | ||
if j < 0 || j >= len(data[i]) { | ||
return 0 | ||
return zeroValue, false | ||
} | ||
return data[i][j] | ||
return data[i][j], true | ||
} |