Maybe I'm misunderstanding, but the important thing is that foo never be 
copied. In your example, it is, in fact, *not *copied. See 
https://play.golang.org/p/U5EsDZSO3ce. Note that Foo is copied, but both f2 
and f3 contain the same foo pointer, so the foo was never copied or moved. 
When embedding a *foo, there is no real reason to use a *Foo, and the 
example could be - https://play.golang.org/p/RapUv6AvPK-,

On Sunday, February 24, 2019 at 5:34:27 PM UTC-5, Deomid Ryabkov wrote:
>
> thanks for the suggestion Burak and Jake, however embedding won't do for 
> the same reason as aliasing - it allows copying by value, which i want to 
> prevent (in my real use case foo.bar is a C pointer).
>
> https://play.golang.org/p/4BcOQTGkPiy works, and it should not (f3 := *f2 
> is a no as it makes copy of f2.bar)
>
> On Sunday, February 24, 2019 at 10:07:05 PM UTC, Jake Montgomery wrote:
>>
>> On Sunday, February 24, 2019 at 4:31:10 PM UTC-5, Deomid Ryabkov wrote:
>>>
>>> Why can't Go resolve methods declared on the pointer receiver of the 
>>> destination type?
>>>
>>  
>> Because that  is not how the language was designed. (I'm too busy to look 
>> up the spec here, but it is in there.)
>>
>>>
>>> consider:
>>>
>>> [SNIP]
>>>
>>> (playground link: https://play.golang.org/p/v0f9pYaTJAa )
>>>
>>> it fails to compile with "prog.go:25:23: f2.GetBar undefined (type Foo 
>>> has no field or method GetBar)" but it's not clear to me, why Foo doesn't 
>>> get pointer methods from *foo.
>>>
>>>  
>> You could try using an embedded type to have Foo "inherit" methods from 
>> *foo, like this - https://play.golang.org/p/WvNj5GUjsxe. 
>>
>> Good Luck
>>
>

-- 
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