Skip to content

Commit bbd175c

Browse files
committed
Add ptr.NonZero to return ptr if value is not zero
1 parent 011123e commit bbd175c

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

ptr/ptr.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ func Ptr[T any](val T) *T {
1818
return &val
1919
}
2020

21+
func NonZero[T comparable](val T) *T {
22+
var zero T
23+
if val == zero {
24+
return nil
25+
}
26+
return &val
27+
}
28+
2129
func Val[T any](ptr *T) (val T) {
2230
if ptr != nil {
2331
val = *ptr

0 commit comments

Comments
 (0)