pointers - Why i cant apply address operator (&) to functions return values directly -
this question has answer here:
i don't why following code snippet isn't compiling. compiler states:
cannot take address of getastring()
code:
func getastringpointer() *string { return &getastring() } func getastring() string { return "" }
but, storing results of function in auxliary variable , return address of variable compiler behaves ok.
func getastringpointer() *string { var astring = getastring() return &astring } func getastring() string { return "" }
you can't apply &
value (unless it's composite literal). spec:
for operand x of type t, address operation &x generates pointer of type *t x. 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 may (possibly parenthesized) composite literal. if evaluation of x cause run-time panic, evaluation of &x too.
Comments
Post a Comment