Bryan
Bryan

Reputation: 74

Confusion on how Golang pointers are preserved in structs

I'm currently learning golang (mostly a Java/C# developer) and I hit an issue with pointers and defer.

I'm trying to wrap the writes to a CSV file under a struct in a OO-like style. From the examples I found online, it seems that creating "methods" on a struct could be done like so:

type MyObject struct {
    fp  *os.File
    csv *csv.Writer
}

func (mo MyObject) Open(filepath string) {

    println(&mo)
    var err error
    mo.fp, err = os.Create(filepath)
    if err != nil {
        panic(err)
    }

    mo.csv = csv.NewWriter(mo.fp)
}

The problem I hit was once I left the Open method, the pointers for fp and csv went back to null. Subsequent calls to this class would throw a nil error. A full example can be found here.

After a lot of googling, I ended up looking at how golang implemented their logger. They used a pointer to the object like so:

type MyObject struct {
    fp  *os.File
    csv *csv.Writer
}

func New() *MyObject {
    return &MyObject{}
}

func (mo *MyObject) Open(filepath string) {

    println(&mo)
    var err error
    mo.fp, err = os.Create(filepath)
    if err != nil {
        panic(err)
    }

    mo.csv = csv.NewWriter(mo.fp)
}

A refactoring of my code (see here) shows it works as expected. I'm still confused though why the first method didn't work. I'm guessing I'm misunderstanding something on how structs, pointers, and/or defer work. What am I missing?

Upvotes: 1

Views: 129

Answers (1)

Edwin O.
Edwin O.

Reputation: 5276

It didn't work in the first case, because func (mo MyObject) Open(filepath string) only got a local copy of MyObject...and all changes made to it remained within that context.

But after you added * to the receiver, i.e (mo *MyObject) the changes within the function affected the original MyObject.

you can check here for more info

hope this helps

Upvotes: 4

Related Questions