File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11# pointer
2- [ ![ GoDoc] ( https://godoc.org/github.com/AlekSi/pointer?status.svg )] ( https://godoc.org/github.com/AlekSi/pointer )
3- [ ![ Build Status] ( https://travis-ci.org/AlekSi/pointer.svg )] ( https://travis-ci.org/AlekSi/pointer )
42
5- Go package pointer provides helpers to get pointers to values of built-in types.
3+ [ ![ Go Reference] ( https://pkg.go.dev/badge/github.com/AlekSi/pointer.svg )] ( https://pkg.go.dev/github.com/AlekSi/pointer )
4+
5+ Go package ` pointer ` provides helpers to convert between pointers and values of built-in
6+ (and, with Go 1.18+ generics, of any) types.
67
78```
89go get github.com/AlekSi/pointer
910```
1011
11- API is stable. [ Documentation] ( http ://godoc.org /github.com/AlekSi/pointer) .
12+ API is stable. [ Documentation] ( https ://pkg.go.dev /github.com/AlekSi/pointer) .
1213
1314``` go
1415package motivationalexample
Original file line number Diff line number Diff line change 33
44package pointer
55
6+ // To returns a pointer to the passed value.
67func To [T any ](t T ) * T {
78 return & t
89}
910
11+ // ToOrNil returns a pointer to the passed value, or nil, if the passed value is a zero value.
12+ // If the passed value has `IsZero() bool` method (for example, time.Time instance),
13+ // it is used to determine if the value is zero.
1014func ToOrNil [T comparable ](t T ) * T {
1115 if z , ok := any (t ).(interface { IsZero () bool }); ok {
1216 if z .IsZero () {
@@ -22,6 +26,7 @@ func ToOrNil[T comparable](t T) *T {
2226 return & t
2327}
2428
29+ // Get returns the value from the passed pointer or the zero value if the pointer is nil.
2530func Get [T any ](t * T ) T {
2631 if t == nil {
2732 var zero T
Original file line number Diff line number Diff line change 1- // Package pointer provides helpers to get pointers to values of built-in types.
1+ // Package pointer provides helpers to convert between pointers and values of built-in (and, with generics, of any) types.
22package pointer // import "github.com/AlekSi/pointer"
33
44import (
You can’t perform that action at this time.
0 commit comments