Skip to content

Commit

Permalink
a little refactoring and now pointers are live
Browse files Browse the repository at this point in the history
  • Loading branch information
quii committed Apr 1, 2018
1 parent 846b4dc commit 6128922
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
16 changes: 9 additions & 7 deletions pointers/readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Pointers and errors (WIP)
# Pointers and errors

We learned about structs in the last section which let us capture a number of values related around a concept.

Expand Down Expand Up @@ -343,20 +343,21 @@ Let's try this out in a test

```go
t.Run("Withdraw insufficient funds", func(t *testing.T) {
wallet := Wallet{Bitcoin(20)}
startingBalance := Bitcoin(20)
wallet := Wallet{startingBalance}
err := wallet.Withdraw(Bitcoin(100))

assertBalance(t, wallet, Bitcoin(20))
assertBalance(t, wallet, startingBalance)

if err == nil {
t.Error("wanted an error but didn't get one")
}
})
```

We want `Withdraw` to return an error _if_ you try and take out more than you have.
We want `Withdraw` to return an error _if_ you try and take out more than you have and the balance should stay the same

We then check it has returned it by failing the test if it is `nil`
We then check an error has returned by failing the test if it is `nil`.

`nil` is synonymous with `null` from other programming languages. Errors can be `nil` because the return type of `Widthdraw` will be `error`, which is an interface. If you see a function that takes arguments or returns values that are interfaces, they can be nillable.

Expand Down Expand Up @@ -445,10 +446,11 @@ And then update the caller

```go
t.Run("Withdraw insufficient funds", func(t *testing.T) {
wallet := Wallet{Bitcoin(20)}
startingBalance := Bitcoin(20)
wallet := Wallet{startingBalance}
err := wallet.Withdraw(Bitcoin(100))

assertBalance(t, wallet, Bitcoin(20))
assertBalance(t, wallet, startingBalance)
assertError(t, err, "cannot withdraw, insufficient funds")
})
```
Expand Down
5 changes: 3 additions & 2 deletions pointers/v3/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ func TestWallet(t *testing.T) {
})

t.Run("Withdraw insufficient funds", func(t *testing.T) {
wallet := Wallet{Bitcoin(20)}
startingBalance := Bitcoin(20)
wallet := Wallet{startingBalance}
err := wallet.Withdraw(Bitcoin(100))

assertBalance(t, wallet, Bitcoin(20))
assertBalance(t, wallet, startingBalance)
assertError(t, err)
})
}
5 changes: 3 additions & 2 deletions pointers/v4/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ func TestWallet(t *testing.T) {
})

t.Run("Withdraw insufficient funds", func(t *testing.T) {
wallet := Wallet{Bitcoin(20)}
startingBalance := Bitcoin(20)
wallet := Wallet{startingBalance}
err := wallet.Withdraw(Bitcoin(100))

assertBalance(t, wallet, Bitcoin(20))
assertBalance(t, wallet, startingBalance)
assertError(t, err, InsufficientFundsError)
})
}
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ If there's no link, it's not done yet! [why not contribute?](contributing.md)
3. [Iteration](/for) - Learn about `for` and benchmarking.
4. [Arrays and slices](/arrays) - Learn about arrays, slices, `len`, varargs, `range` and test coverage.
5. [Structs, methods & interfaces](/structs) - Learn about `struct`, methods, `interface` and table driven tests.
6. [Pointers & errors (WIP)](/pointers) - Learn about pointers, errors and type aliasing.
6. [Pointers & errors](/pointers) - Learn about pointers, errors and type aliasing.
7. [Dependency Injection & interfaces (WIP)](/di-and-interfaces) - Learn about dependency injection, how it relates to using interfaces and a primer on io
8. Concurrency (WIP)
9. Errors
Expand Down

0 comments on commit 6128922

Please sign in to comment.