github.com/clubpay/ronykit/kit@v0.14.4-0.20240515065620-d0dace45cbc7/utils/generic.go (about) 1 package utils 2 3 // PtrVal returns the value of the pointer src. It is a dereference operation. 4 func PtrVal[T any](src *T) T { 5 if src == nil { 6 var dst T 7 8 return dst 9 } 10 11 return *src 12 } 13 14 // ValPtr returns the pointer of the src. It is a reference operation. 15 func ValPtr[T any](src T) *T { 16 return &src 17 } 18 19 // ValPtrOrNil returns the pointer of the src if src is not zero value, otherwise nil. 20 func ValPtrOrNil[T comparable](src T) *T { 21 var zero T 22 if src == zero { 23 return nil 24 } 25 26 return &src 27 } 28 29 // Must panics if err is not nil 30 func Must[T any](src T, err error) T { 31 if err != nil { 32 panic(err) 33 } 34 35 return src 36 } 37 38 // Ok returns the value and ignores the error 39 func Ok[T any](v T, _ error) T { 40 return v 41 } 42 43 // OkOr returns the value if err is nil, otherwise returns the fallback value 44 func OkOr[T any](v T, err error, fallback T) T { 45 if err != nil { 46 return fallback 47 } 48 49 return v 50 }