From 974a39b90e4b1293f3bf4edef9c41aed44a0c56e Mon Sep 17 00:00:00 2001 From: Alexey Palazhchenko Date: Fri, 22 Oct 2021 20:49:14 +0300 Subject: [PATCH] Update docs --- README.md | 9 +++++---- generic.go | 5 +++++ pointer.go | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 25f2948..64c72d7 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/generic.go b/generic.go index d917ea8..1ef976b 100644 --- a/generic.go +++ b/generic.go @@ -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() { @@ -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 diff --git a/pointer.go b/pointer.go index 15ce5c4..8203feb 100644 --- a/pointer.go +++ b/pointer.go @@ -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 (