Skip to content

Commit 1e4dfca

Browse files
committed
add ptr func
1 parent 8b30ec9 commit 1e4dfca

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module github.com/flow-lab/utils
22

33
go 1.20
44

5-
require github.com/stretchr/testify v1.8.3
5+
require github.com/stretchr/testify v1.8.4
66

77
require (
88
github.com/davecgh/go-spew v1.1.1 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
22
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
33
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
44
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5-
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
6-
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
5+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
6+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
77
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
88
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
99
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

utils.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,9 @@ func Short(s string) string {
105105
}
106106
return s
107107
}
108+
109+
// Ptr returns a pointer to the given value.
110+
// Useful for passing values to functions that expect pointers.
111+
func Ptr[T any](value T) *T {
112+
return &value
113+
}

utils_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,15 @@ func TestUtils(t *testing.T) {
7979
assert.Equal(t, "1234", Short("1234"))
8080
assert.Equal(t, "", Short(""))
8181
})
82+
83+
t.Run("Test2Ptr", func(t *testing.T) {
84+
var i int64 = 1234
85+
assert.Equal(t, &i, Ptr(i))
86+
var f float32 = 1234
87+
assert.Equal(t, &f, Ptr(f))
88+
var b bool = true
89+
assert.Equal(t, &b, Ptr(b))
90+
var s string = "1234"
91+
assert.Equal(t, &s, Ptr(s))
92+
})
8293
}

0 commit comments

Comments
 (0)