File: ptr.go

package info (click to toggle)
golang-bugst-f 0.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 108 kB
  • sloc: makefile: 2
file content (16 lines) | stat: -rw-r--r-- 277 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package f

// Ptr returns a pointer to v.
func Ptr[T any](v T) *T {
	return &v
}

// UnwrapOrDefault returns the ptr value if it is not nil, otherwise returns the zero value.
func UnwrapOrDefault[T any](ptr *T) T {
	if ptr != nil {
		return *ptr
	}

	var zero T
	return zero
}