Skip to content

Commit

Permalink
Make the struct pointer section more explicit (quii#207)
Browse files Browse the repository at this point in the history
* Make the struct pointer section more explicit

It took me a while crawling through the Go documentation to understand this black magic.

* Match markdown nomenclature of this doc

* Reuse the wallet example in (*w) dereference
  • Loading branch information
ʇɹǝqƃǝᴉs puɐloɹ authored and quii committed Jul 16, 2019
1 parent e290f12 commit ec54187
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pointers-and-errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,18 @@ The difference is the receiver type is `*Wallet` rather than `Wallet` which you

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:

```go
func (w *Wallet) Balance() int {
return (*w).balance
}
```

and seemingly addressed the object directly. In fact, the code above using `(*w)` is absolutely valid. However, the makers of Go deemed this notation cumbersome, so the language permits us to write `w.balance`, without explicit dereference.
These pointers to structs even have an own name: _struct pointers_ and they are [automatically dereferenced](https://golang.org/ref/spec#Method_values).


## Refactor

We said we were making a Bitcoin wallet but we have not mentioned them so far. We've been using `int` because they're a good type for counting things!
Expand Down

0 comments on commit ec54187

Please sign in to comment.