diff --git a/generic.go b/generic.go index fdf6f48..d917ea8 100644 --- a/generic.go +++ b/generic.go @@ -1,3 +1,6 @@ +//go:build go1.18 +// +build go1.18 + package pointer func To[T any](t T) *T { @@ -5,7 +8,7 @@ func To[T any](t T) *T { } func ToOrNil[T comparable](t T) *T { - if z, ok := interface{}(t).(interface{ IsZero() bool }); ok { + if z, ok := any(t).(interface{ IsZero() bool }); ok { if z.IsZero() { return nil } diff --git a/generic_test.go b/generic_test.go new file mode 100644 index 0000000..154a59b --- /dev/null +++ b/generic_test.go @@ -0,0 +1,33 @@ +//go:build go1.18 +// +build go1.18 + +package pointer + +import ( + "testing" + "time" +) + +func TestGeneric(t *testing.T) { + var x time.Time + if *To(x) != x { + t.Errorf("*To(%v)", x) + } + if ToOrNil(x) != nil { + t.Errorf("ToOrNil(%v)", x) + } + if Get((*time.Time)(nil)) != x { + t.Errorf("Time(%v)", nil) + } + + x = time.Date(2014, 6, 25, 12, 24, 40, 0, time.UTC) + if *To(x) != x { + t.Errorf("*To(%v)", x) + } + if *ToOrNil(x) != x { + t.Errorf("*ToOrNil(%v)", x) + } + if Get(&x) != x { + t.Errorf("Get(%v)", &x) + } +} diff --git a/go.mod b/go.mod index 1d40808..07e2025 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/AlekSi/pointer -go 1.11 +go 1.18