Given a struct
that looks like
type foo struct {
i *int
}
if I want to set i
to 1, I must
throwAway := 1
instance := foo { i: &throwAway }
Is there any way to do this in a single line without having to give my new i
value it's own name (in this case throwaway
)?
As pointed in the mailing list, you can just do this:
func intPtr(i int) *int {
return &i
}
and then
instance := foo { i: intPtr(1) }
if you have to do it often. intPtr
gets inlined (see go build -gcflags '-m'
output), so it should have next to no performance penalty.
No this is not possible to do in one line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With