Skip to content

Commit

Permalink
adding safe dereferencing helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Mzack9999 committed May 5, 2023
1 parent f4baa90 commit 852aa89
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions ptr/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ptr

// Safe dereferences safely a pointer
// - if the pointer is nil => returns the zero value of the type of the pointer if nil
// - if the pointer is not nil => returns the dereferenced pointer
func Safe[T any](v *T) T {
if v == nil {
return *new(T)
}
return *v
}

0 comments on commit 852aa89

Please sign in to comment.