A question about copying

84 views
Skip to first unread message

chri...@surlykke.dk

unread,
Jul 25, 2020, 4:09:29 AM7/25/20
to golang-nuts
When running this program:

package main

import (
"fmt"
)

type Foo struct {
i int
}

func main() {
var f1 = &Foo{i: 0}
var f2 = &(*f1)
f2.i = 1
fmt.Println(f1, f2)
}

it yields:

&{1} &{1} 


I (naively) assumed that the expression

&(*f1) 

would, first, create a copy of *f1, and then a pointer to that copy, but evidently f2 becomes a pointer to the same struct as f1. Is this something I should have deduced from the language spec?

best regards Christian Surlykke


Dan Kortschak

unread,
Jul 25, 2020, 4:54:12 AM7/25/20
to golang-nuts
On Sat, 2020-07-25 at 01:09 -0700, chri...@surlykke.dk wrote:
> &(*f1)
>
> would, first, create a copy of *f1, and then a pointer to that copy,
> but evidently f2 becomes a pointer to the same struct as f1. Is this
> something I should have deduced from the language spec?

&(*p) says "give me the address of the thing that is pointed to by p".
This is p.

If you assign the value of *p to another variable and take the address
of that, then you'll get the outcome you were wanting.



Aleksey Tulinov

unread,
Jul 25, 2020, 4:55:12 AM7/25/20
to chri...@surlykke.dk, golang-nuts
You are probably thinking about code like this:

var f2 = *f1

Which will make a copy, although not because `f1` is dereferenced, but
because `=` was called on a value.

Dereferencing a pointer gives a reference to the same value, taking
address of the same value will produce a pointer to the same value. So
in expression like this:

var f2 = &(*f1)

`&` and `*` cancel each other out and it can be simplified to:

var f2 = f1

Which means "make a copy of pointer". If you take the address of `f1`
and `f2` you'll see that those are indeed different pointers.

If you're thinking about something like `f(*f1)`, then I believe the
function call will make a copy because arguments are passed by values.
`f(f1)` will make a copy too, but a copy of pointer.

Does that make sense?

сб, 25 июл. 2020 г. в 11:09, chri...@surlykke.dk <chri...@surlykke.dk>:
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/f427ddf6-f4fc-42f4-bee8-d1dab0fef566n%40googlegroups.com.

Jan Mercl

unread,
Jul 25, 2020, 4:56:35 AM7/25/20
to chri...@surlykke.dk, golang-nuts
On Sat, Jul 25, 2020 at 10:09 AM chri...@surlykke.dk
<chri...@surlykke.dk> wrote:

> Is this something I should have deduced from the language spec?

https://golang.org/ref/spec#Address_operators

""""
For an operand x of type T, the address operation &x generates a
pointer of type *T to x. The operand must be addressable, that is,
either a variable, pointer indirection, or slice indexing operation;
or a field selector of an addressable struct operand; or an array
indexing operation of an addressable array.
""""

Note the 'pointer indirection'.

chri...@surlykke.dk

unread,
Jul 25, 2020, 9:17:26 AM7/25/20
to golang-nuts
Aha, I see. Thanks for explaining.

br. Chr.

Reply all
Reply to author
Forward
0 new messages