You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

23 lines
432 B
Go

package ref
import (
"fmt"
)
// New creates a reference for a given pointer
func New[T any](v *T) Ref[T] {
if v == nil {
var zero T
return Ref[T]{ptr: &zero}
}
return Ref[T]{ptr: v}
}
// Ref is a read reference to some value T
type Ref[T any] struct { ptr *T }
// Val reads the value for this reference
func (r Ref[T]) Val() T { return *r.ptr }
func (r Ref[T]) String() string { return fmt.Sprintf("ref{%s}", *r.ptr) }