pointers - How do I do a literal *int64 in Go? -
i have struct type *int64
field.
type sometype struct { somefield *int64 }
at point in code, want declare literal of (say, when know said value should 0, or pointing 0, know mean)
instance := sometype{ somefield: &0, }
...except doesn't work
./main.go:xx: cannot use &0 (type *int) type *int64 in field value
so try this
instance := sometype{ somefield: &int64(0), }
...but doesn't work
./main.go:xx: cannot take address of int64(0)
how do this? solution can come using placeholder variable
var placeholder int64 placeholder = 0 instance := sometype{ somefield: &placeholder, }
note: edit: no not. sorry this.&0
syntax works fine when it's *int instead of *int64
.
edit:
aparently there ambiguity question. i'm looking way literally state *int64
. used inside constructor, or state literal struct values, or arguments other functions. helper functions or using different type not solutions i'm looking for.
the go language specification (address operators) not allow take address of numeric constant (not of untyped nor of typed constant).
the operand must addressable, is, either variable, pointer indirection, or slice indexing operation; or field selector of addressable struct operand; or array indexing operation of addressable array. exception addressability requirement,
x
[in expression of&x
] may (possibly parenthesized) composite literal.
for reasoning why isn't allowed, see related question: find address of constant in go. similar question (similarly not allowed take address): how can store reference result of operation in go?
your options (try on go playground):
1) new()
you can use builtin new()
function allocate new zero-valued int64
, address:
instance := sometype{ somefield: new(int64), }
but note can used allocate , obtain pointer 0 value of type.
2) slice literal, indexing , taking address
if want *somefield
other 0
, need addressable.
you can still that, that's ugly:
instance2 := sometype{ somefield: &[]int64{2}[0], } fmt.println(*instance2.somefield) // prints 2
what happens here []int64
slice created literal, having 1 element (2
). , indexed (0th element) , address of 0th element taken. in background array of [1]int64
allocated , used backing array slice. there lot of boilerplate here.
3) helper variable
simplest , recommended non-zero elements use helper variable address can taken:
helper := int64(3) instance3 := sometype{ somefield: &helper, }
4) helper function
or if need many times, can create helper function allocates , returns *int64
:
func create(x int64) *int64 { return &x }
and using it:
instance4 := sometype{ somefield: create(4), }
5) one-liner anonymous function
instance5 := sometype{ somefield: func() *int64 { := int64(5); return &i }(), }
or (shorter) alternative:
instance5 := sometype{ somefield: func(i int64) *int64 { return &i }(5), }
6) helper struct literal
let's examine exception addressability requirements:
as exception addressability requirement,
x
[in expression of&x
] may (possibly parenthesized) composite literal.
this means taking address of composite literal, e.g. struct literal ok. if so, have struct value allocated , pointer obtained it. if so, requirement become available us: "field selector of addressable struct operand". if struct literal contains field of type int64
, can take address of field!
let's see option in action. use wrapper struct type:
type intwrapper struct { x int64 }
and can do:
i6 := sometype{ somefield: &(&intwrapper{6}).x, }
note this
&(&intwrapper{6}).x
means following:
& ( (&intwrapper{6}).x )
but can omit "outer" parenthesis address operator &
applied result of selector expression.
also note in background following happen (this valid syntax):
&(*(&intwrapper{6})).x
7) helper anonymous struct literal
the principle same case #6, can use anonymous struct literal, no helper/wrapper struct type definition needed:
i7 := sometype{ somefield: &(&struct{ x int64 }{7}).x, }
Comments
Post a Comment