Created
March 16, 2022 12:57
-
-
Save ivanvanderbyl/fad86c1359671ca1976f191e80021a5d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package ptr | |
// Safe reads the point and returns the zero value of the type of | |
// the pointer is nil, otherwise it returns the value of the pointer. | |
// Usage: | |
// ptr.Safe(obj.SomePointerField) | |
func Safe[T any](v *T) T { | |
if v == nil { | |
return *new(T) | |
} | |
return *v | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment