Skip to content

pointers and errors #301

@Leamsi9

Description

@Leamsi9

The course states:

Pointers let us point to some values and then let us change them. So rather than taking a copy of the Wallet, we take a pointer to the wallet so we can change it.

func (w *Wallet) Deposit(amount int) {
w.balance += amount
}

func (w *Wallet) Balance() int {
return w.balance
}
The difference is the receiver type is *Wallet rather than Wallet which you can read as "a pointer to a wallet".

Try and re-run the tests and they should pass.

Now you might wonder, why did they pass? We didn't dereference the pointer in the function, like so:

func (w *Wallet) Balance() int {
return (*w).balance
}
and seemingly addressed the object directl

However in this example, it seems that you only need the pointer in Deposit(), which is changing the balance, and not in Balance() which is merely returning it. Indeed, if you take out the asterisk in the former, it breaks, but if you take it out in the latter it still passes. Given you highlight the pointer in Balance() rather than in Deposit(), this could do with explaining/revising?

Thank you so much for a brilliant course.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions