Skip to content

Commit

Permalink
ptr: add NonZero to return pointer if value is not zero
Browse files Browse the repository at this point in the history
  • Loading branch information
hifi committed Aug 19, 2024
1 parent 011123e commit 9fa841e
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ptr/ptr.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ func Ptr[T any](val T) *T {
return &val
}

func NonZero[T comparable](val T) *T {
var zero T
if val == zero {
return nil
}
return &val
}

func Val[T any](ptr *T) (val T) {
if ptr != nil {
val = *ptr
Expand Down

0 comments on commit 9fa841e

Please sign in to comment.