If you create a wrapper interface type and create *generic constraints* with
the *MyWrapper* struct then it'll type cast with *interface* with *(T)*.

https://go.dev/play/p/-VnQxG77sDL


On Fri, Nov 25, 2022 at 3:38 PM 'Axel Wagner' via golang-nuts <
golang-nuts@googlegroups.com> wrote:

> If you assign a value to an interface, it will get copied. That's the same
> as if you assign an int to another int - it will get copied. If you want
> the interface to contain a reference, you have to assign a pointer to it:
> https://go.dev/play/p/nLw51pjWh4u
>
> Side note: Go doesn't have "instances". It has "variables", which is
> *probably* what you mean.
>
> On Fri, Nov 25, 2022 at 10:55 AM Denis P <denis.puj...@gmail.com> wrote:
>
>> Thank you everyone for your help.
>> The problem still exists.
>> I am looking for a solution where s and s2 point to the same instance.
>> The current example proves they are different instances.
>> https://go.dev/play/p/_JyfJelhIy4
>>
>> The main goal is to store any type in a wrapper and then get the
>> reference of the actual struct.
>> Also it must be the same instance, so the println should print 2, 2, 2
>>
>> But look like there is no obvious solution. :-(
>>
>> On Friday, 25 November 2022 at 08:19:17 UTC Brian Candler wrote:
>>
>>> To give a real-world example:
>>>
>>> https://pkg.go.dev/github.com/prometheus/client_golang/prometheus
>>>
>>> type metrics struct {
>>>         cpuTemp    prometheus.Gauge
>>>         hdFailures *prometheus.CounterVec
>>> }
>>>
>>> Question: why is prometheus.Gauge not a pointer, but
>>> *prometheus.CounterVec is a pointer?
>>>
>>> Answer: because prometheus.Gauge is an interface, whereas
>>> prometheus.CounterVec is a struct.
>>>
>>> An interface essentially already *contains* a pointer to a data value -
>>> in fact, a tuple of (type, pointer).  So you should never take a pointer to
>>> an interface.  Just pass the interface value, and it will copy the (type,
>>> pointer) pair.
>>>
>>> However, when you extract a value from an interface, it *will* always
>>> copy the internal value.  This avoids aliasing issues: someone who passes
>>> an interface value to someone else, won't expect the recipient to be able
>>> to change the sender's copy.
>>>
>>> e.g.
>>> https://go.dev/play/p/u5y3Kwm9Ydz
>>>
>>> Of course, if your interface *contains* a pointer, then the recipient of
>>> the interface can modify the thing being pointed to.
>>> https://go.dev/play/p/o_XAJtNuyGF
>>>
>>> But they can't modify the pointer itself held within the interface, to
>>> make it point to something else.
>>>
>>> The short version is: avoid pointers to interfaces, as the FAQ says.
>>> Instead, let a concrete pointer value satisfy an interface.
>>>
>>> On Friday, 25 November 2022 at 07:40:59 UTC Nigel Tao wrote:
>>>
>>>> Possibly relevant:
>>>> https://go.dev/doc/faq#pointer_to_interface
>>>>
>>> --
>> 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.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/golang-nuts/948dc8f3-385e-4f50-a44b-ddf4792e6362n%40googlegroups.com
>> <https://groups.google.com/d/msgid/golang-nuts/948dc8f3-385e-4f50-a44b-ddf4792e6362n%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/CAEkBMfE7XX%2BnHJTW36GaoLjQWidY7GCjpHP1zyOJJzMcSq13JA%40mail.gmail.com
> <https://groups.google.com/d/msgid/golang-nuts/CAEkBMfE7XX%2BnHJTW36GaoLjQWidY7GCjpHP1zyOJJzMcSq13JA%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>


-- 


Thanks & Regards
Deeptiman Pattnaik

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAAWK-epK544zJnjnJff99sA%3DbJiKOPftv_3ydgOsaRqo38Ch%2Bw%40mail.gmail.com.

Reply via email to