File tree Expand file tree Collapse file tree 3 files changed +11
-5
lines changed Expand file tree Collapse file tree 3 files changed +11
-5
lines changed Original file line number Diff line number Diff line change 1
1
# 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 )
4
2
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.
6
7
7
8
```
8
9
go get github.com/AlekSi/pointer
9
10
```
10
11
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) .
12
13
13
14
``` go
14
15
package motivationalexample
Original file line number Diff line number Diff line change 3
3
4
4
package pointer
5
5
6
+ // To returns a pointer to the passed value.
6
7
func To [T any ](t T ) * T {
7
8
return & t
8
9
}
9
10
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.
10
14
func ToOrNil [T comparable ](t T ) * T {
11
15
if z , ok := any (t ).(interface { IsZero () bool }); ok {
12
16
if z .IsZero () {
@@ -22,6 +26,7 @@ func ToOrNil[T comparable](t T) *T {
22
26
return & t
23
27
}
24
28
29
+ // Get returns the value from the passed pointer or the zero value if the pointer is nil.
25
30
func Get [T any ](t * T ) T {
26
31
if t == nil {
27
32
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.
2
2
package pointer // import "github.com/AlekSi/pointer"
3
3
4
4
import (
You can’t perform that action at this time.
0 commit comments