diff options
Diffstat (limited to 'modules/util')
-rw-r--r-- | modules/util/util.go | 5 | ||||
-rw-r--r-- | modules/util/util_test.go | 9 |
2 files changed, 14 insertions, 0 deletions
diff --git a/modules/util/util.go b/modules/util/util.go index 3051449544..9d5e6c1e89 100644 --- a/modules/util/util.go +++ b/modules/util/util.go @@ -256,3 +256,8 @@ func ToFloat64(number any) (float64, error) { } return value, nil } + +// ToPointer returns the pointer of a copy of any given value +func ToPointer[T any](val T) *T { + return &val +} diff --git a/modules/util/util_test.go b/modules/util/util_test.go index 8cceafa2f6..c5830ce01c 100644 --- a/modules/util/util_test.go +++ b/modules/util/util_test.go @@ -224,3 +224,12 @@ func TestToTitleCase(t *testing.T) { assert.Equal(t, ToTitleCase(`foo bar baz`), `Foo Bar Baz`) assert.Equal(t, ToTitleCase(`FOO BAR BAZ`), `Foo Bar Baz`) } + +func TestToPointer(t *testing.T) { + assert.Equal(t, "abc", *ToPointer("abc")) + assert.Equal(t, 123, *ToPointer(123)) + abc := "abc" + assert.False(t, &abc == ToPointer(abc)) + val123 := 123 + assert.False(t, &val123 == ToPointer(val123)) +} |