FAQ
Please take a look at the simplified example:
http://play.golang.org/p/1eMJs_LyCE

Using the interface as the type passed around in function calls, i loose
the value set in the Init function. My first thought was to set the
interface methods as pointer recievers. However then the compiler complains
that my struct C doesnt implement the interface.

How can this be resolved so the function T2 retains the value set in T1?

--

Search Discussions

  • John Asmuth at Nov 19, 2012 at 7:59 pm
    var t T = &C{}

    Otherwise you're just copying around the C whenever you do something with
    it. If you use &C{}, you're copying around a pointer to the C, which allows
    changes to the C to be reflected elsewhere.
    On Monday, November 19, 2012 2:54:10 PM UTC-5, netbrain wrote:

    Please take a look at the simplified example:
    http://play.golang.org/p/1eMJs_LyCE

    Using the interface as the type passed around in function calls, i loose
    the value set in the Init function. My first thought was to set the
    interface methods as pointer recievers. However then the compiler complains
    that my struct C doesnt implement the interface.

    How can this be resolved so the function T2 retains the value set in T1?
    --
  • Francesc Campoy Flores at Nov 19, 2012 at 8:17 pm
    Your Init method modifies the receiver, therefore it should be a pointer
    type. Otherwise your change is being done in a copy of c.

    http://play.golang.org/p/UMMaUfRC7K

    On Mon, Nov 19, 2012 at 11:59 AM, John Asmuth wrote:

    var t T = &C{}

    Otherwise you're just copying around the C whenever you do something with
    it. If you use &C{}, you're copying around a pointer to the C, which allows
    changes to the C to be reflected elsewhere.

    On Monday, November 19, 2012 2:54:10 PM UTC-5, netbrain wrote:

    Please take a look at the simplified example: http://play.golang.**
    org/p/1eMJs_LyCE <http://play.golang.org/p/1eMJs_LyCE>

    Using the interface as the type passed around in function calls, i loose
    the value set in the Init function. My first thought was to set the
    interface methods as pointer recievers. However then the compiler complains
    that my struct C doesnt implement the interface.

    How can this be resolved so the function T2 retains the value set in T1?
    --



    --
    --
    Francesc

    --
  • Kim Eik at Nov 19, 2012 at 8:28 pm
    Great, i think i understand now. Thank you.


    On Mon, Nov 19, 2012 at 9:17 PM, Francesc Campoy Flores
    wrote:
    Your Init method modifies the receiver, therefore it should be a pointer
    type. Otherwise your change is being done in a copy of c.

    http://play.golang.org/p/UMMaUfRC7K

    On Mon, Nov 19, 2012 at 11:59 AM, John Asmuth wrote:

    var t T = &C{}

    Otherwise you're just copying around the C whenever you do something with
    it. If you use &C{}, you're copying around a pointer to the C, which allows
    changes to the C to be reflected elsewhere.

    On Monday, November 19, 2012 2:54:10 PM UTC-5, netbrain wrote:

    Please take a look at the simplified example: http://play.golang.**
    org/p/1eMJs_LyCE <http://play.golang.org/p/1eMJs_LyCE>

    Using the interface as the type passed around in function calls, i loose
    the value set in the Init function. My first thought was to set the
    interface methods as pointer recievers. However then the compiler complains
    that my struct C doesnt implement the interface.

    How can this be resolved so the function T2 retains the value set in T1?
    --



    --
    --
    Francesc

    --

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 19, '12 at 7:54p
activeNov 19, '12 at 8:28p
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2017 Grokbase