Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekSi committed Oct 22, 2021
1 parent a285f58 commit 974a39b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# pointer
[![GoDoc](https://godoc.org/github.com/AlekSi/pointer?status.svg)](https://godoc.org/github.com/AlekSi/pointer)
[![Build Status](https://travis-ci.org/AlekSi/pointer.svg)](https://travis-ci.org/AlekSi/pointer)

Go package pointer provides helpers to get pointers to values of built-in types.
[![Go Reference](https://pkg.go.dev/badge/github.com/AlekSi/pointer.svg)](https://pkg.go.dev/github.com/AlekSi/pointer)

Go package `pointer` provides helpers to convert between pointers and values of built-in
(and, with Go 1.18+ generics, of any) types.

```
go get github.com/AlekSi/pointer
```

API is stable. [Documentation](http://godoc.org/github.com/AlekSi/pointer).
API is stable. [Documentation](https://pkg.go.dev/github.com/AlekSi/pointer).

```go
package motivationalexample
Expand Down
5 changes: 5 additions & 0 deletions generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@

package pointer

// To returns a pointer to the passed value.
func To[T any](t T) *T {
return &t
}

// ToOrNil returns a pointer to the passed value, or nil, if the passed value is a zero value.
// If the passed value has `IsZero() bool` method (for example, time.Time instance),
// it is used to determine if the value is zero.
func ToOrNil[T comparable](t T) *T {
if z, ok := any(t).(interface{ IsZero() bool }); ok {
if z.IsZero() {
Expand All @@ -22,6 +26,7 @@ func ToOrNil[T comparable](t T) *T {
return &t
}

// Get returns the value from the passed pointer or the zero value if the pointer is nil.
func Get[T any](t *T) T {
if t == nil {
var zero T
Expand Down
2 changes: 1 addition & 1 deletion pointer.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Package pointer provides helpers to get pointers to values of built-in types.
// Package pointer provides helpers to convert between pointers and values of built-in (and, with generics, of any) types.
package pointer // import "github.com/AlekSi/pointer"

import (
Expand Down

0 comments on commit 974a39b

Please sign in to comment.