Hi. Don't worry, I do get pointers and pass-by-value.

I expected the expression (*x) to yield a copy to the pointed-to value.

But, as you say, that happens on assignment.

On Wednesday, March 28, 2018 at 6:50:06 PM UTC+2, Devon H. O'Dell wrote:
>
> 2018-03-28 9:39 GMT-07:00 Devon H. O'Dell <devon...@gmail.com 
> <javascript:>>: 
> > CopyExplicitDeref gets a pointer to the struct in its receiver. If you 
> > have a pointer to T, then taking a pointer to the dereferenced T is a 
> > no-op: you get the pointer of the thing you just dereferenced. Any 
> > statement &*whatever will always yield the value of whatever. Copy 
> > happens on assignment, and no assignment occurs in this statement. 
>
> I just realized that one additional thing that might be confusing is 
> that you're expecting CopyExplicitDeref to get a copy of _something_ 
> since everything in Go is done by-value. Indeed, CopyExplicitDeref 
> does get a copy of something: the pointer to T. So if you change 
> CopyExplicitDeref to: 
>
> func (t *T) CopyExplicitDeref() **T { 
>     return &t 
> } 
>
> and in main: 
>
> a := &T{0} 
> b := a.CopyExplicitDeref() 
> fmt.Println(&a == b) 
>
> you will see that it's actually the pointer that is copied. 
>
> --dho 
>

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to