I do know in Golang it makes use of &
to get a pointer of a variable, not for a relentless:
// incorrect
a := &"take a look at"
For this suituation, we have to outline an additional variable explicity:
// right
str := "take a look at"
a := &str
I attempt to outline a operate to do the changing for each sort:
func PStr(s string) *string { return &s }
func PInt(s int) *int { return &s }
a := &PStr("take a look at")
I considered changing all primitive varieties to a pointer utilizing generic:
sort BasicType interface ~bool
func P[T BasicType](t T) *T { return &t }
a := P("take a look at")
b := P(20)
It really works, however I’m wondering if there’s an present operate in the usual libraries.
How do you wish to use these pointers? You may solely get a pointer to a variable, as a result of it lets you modify the underlying worth. A literal is a continuing expression, so the compiler doesn’t permit you to get a pointer to it.
Notice that in your final instance a
and b
are tips that could copies of "take a look at"
and 20
. That’s since you first go these literals by worth to P
. The worth is copied to t
after which the pointer to t
, not the precise operate argument, is returned.
1 Like
You should use new()
builtin to allocate reminiscence for a variable and get pointer to it in a single command.
1 Like
Thanks in your reply. I simply wish to create a brand new struct in a single line as follows:
sort UserQuery struct {
Age *int
MemoNull *bool
}
question := UserQuery{Age: P(30), MemoNull: P(false)}
Anyway, I discover my reply right here:
it supplies the same method:
func Ptr[T any](v T) *T {
return &v
}
However I nonetheless marvel why the Go workforce doesn’t present such a operate within the SDK or syntax sugar to get the pointer to a literal.
Thanks, Leo, however new
is for a kind not for a literal in my case.
As a result of you aren’t getting a pointer to the literal anyway. Operate Ptr
first instantiates a (native) variable v
from the literal after which returns the pointer to that variable (which in all probability escapes to the heap due to it, BTW).
There merely is not any acquire from this operation. Use pointers for his or her acknowledged functions in Go: passing by reference when you want to modify one thing in a operate or, in uncommon instances, to scale back the price of copying giant information buildings.
1 Like
Nicely, first, I simply wish to get a pointer to a literal string or quantity in a single line as an alternative of
age := 30
memoNull := false
question := UserQuery{Age: &age, MemoNull: &memoNull}
I do know the compiler gained’t allocate reminiscence for the quantity 30
, so I’ve to declare the age
variable explicitly to get a pointer. Thus instantiating a (native) variable v
from the literal is OK for this case.
Second, there’s some logic for every subject in UserQuery to do solely when they don’t seem to be assigned. So I’ve to make use of pointers within the struct to test if the worth is nil
as an alternative of 0 or empty string.
Isn’t it what was added into database/sql
package deal with go 1.22? Null worth, the place you’ll be able to already see if it exists with out checking the default.