Skip to content

Instantly share code, notes, and snippets.

@ivanvanderbyl
Created March 16, 2022 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivanvanderbyl/fad86c1359671ca1976f191e80021a5d to your computer and use it in GitHub Desktop.
Save ivanvanderbyl/fad86c1359671ca1976f191e80021a5d to your computer and use it in GitHub Desktop.
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