Does dereferencing a struct return a new copy of struct?

后端 未结 3 1732
-上瘾入骨i
-上瘾入骨i 2020-12-29 07:50

Why when we reference struct using (*structObj) does Go seem to return a new copy of structObj rather than return the same address of original

3条回答
  •  误落风尘
    2020-12-29 08:43

    No, "assignment" always creates a copy in Go, including assignment to function and method arguments. The statement obj := *p copies the value of *p to obj.

    If you change the statement p.color = "purple" to (*p).color = "purple" you will get the same output, because dereferencing p itself does not create a copy.

提交回复
热议问题