-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Description
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.