From 9a051b3decfd4bab477839a89303500f2a119dda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20Th=C3=B6mmes?= Date: Mon, 27 Jul 2020 10:13:59 +0200 Subject: [PATCH] Add helpers for float pointers. (#1559) --- ptr/ptr.go | 12 ++++++++++++ ptr/ptr_test.go | 20 ++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/ptr/ptr.go b/ptr/ptr.go index a3bfef85c..6d5eeaab6 100644 --- a/ptr/ptr.go +++ b/ptr/ptr.go @@ -30,6 +30,18 @@ func Int64(i int64) *int64 { return &i } +// Float32 is a helper for turning floats into pointers for use in +// API types that want *float32. +func Float32(f float32) *float32 { + return &f +} + +// Float64 is a helper for turning floats into pointers for use in +// API types that want *float64. +func Float64(f float64) *float64 { + return &f +} + // Bool is a helper for turning bools into pointers for use in // API types that want *bool. func Bool(b bool) *bool { diff --git a/ptr/ptr_test.go b/ptr/ptr_test.go index 3555edf2e..f4be88bbc 100644 --- a/ptr/ptr_test.go +++ b/ptr/ptr_test.go @@ -21,9 +21,8 @@ import ( "time" ) -const want = 55 - func TestInt32(t *testing.T) { + const want = 55 gotPtr := Int32(want) if want != *gotPtr { t.Errorf("Int32() = &%v, wanted %v", *gotPtr, want) @@ -31,12 +30,29 @@ func TestInt32(t *testing.T) { } func TestInt64(t *testing.T) { + const want = 55 gotPtr := Int64(want) if want != *gotPtr { t.Errorf("Int64() = &%v, wanted %v", *gotPtr, want) } } +func TestFloat32(t *testing.T) { + const want = 1.25 + gotPtr := Float32(want) + if want != *gotPtr { + t.Errorf("Float32() = &%v, wanted %v", *gotPtr, want) + } +} + +func TestFloat64(t *testing.T) { + const want = 1.25 + gotPtr := Float64(want) + if want != *gotPtr { + t.Errorf("Float64() = &%v, wanted %v", *gotPtr, want) + } +} + func TestBool(t *testing.T) { const want = true gotPtr := Bool(want)