what am i even doing
parent
b79fb00f2e
commit
301da04f9b
@ -0,0 +1,22 @@
|
||||
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) }
|
Loading…
Reference in New Issue