Skip to content

Instantly share code, notes, and snippets.

@ninedraft
Created August 17, 2021 11:46
Show Gist options
  • Save ninedraft/9ab089624cca4a4b1f65cec1d3fdd1e3 to your computer and use it in GitHub Desktop.
Save ninedraft/9ab089624cca4a4b1f65cec1d3fdd1e3 to your computer and use it in GitHub Desktop.
Generic go trash utilities
package value
func If[E any](ok bool, then, or E) E {
if ok {
return then
}
return or
}
func Ptr[E any](v E) *E {
return &v
}
func Deref[E any](v *E, or E) E {
if v == nil {
return or
}
return *v
}
func Empty[E any]() E {
var v E
return v
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment