Skip to content

Commit 2015d60

Browse files
committed
extracted error
1 parent 815d3f6 commit 2015d60

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

pointers/wallet.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@ type Wallet struct {
1616
}
1717

1818
func (w *Wallet) Deposit(amount Bitcoin) {
19-
fmt.Printf("address of balance in Deposit is %v \n", &w.balance)
2019
w.balance += amount
2120
}
2221

2322
func (w *Wallet) Balance() Bitcoin {
2423
return w.balance
2524
}
2625

26+
var ErrInsufficientFunds = errors.New("cannot withdraw, insufficient funds")
27+
2728
func (w *Wallet) Withdraw(amount Bitcoin) error {
2829
if amount > w.balance {
29-
return errors.New("cannot withdraw, insufficient funds")
30+
return ErrInsufficientFunds
3031
}
3132
w.balance -= amount
3233
return nil

pointers/wallet_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ func TestWallet(t *testing.T) {
1515
}
1616
}
1717

18-
assertError := func(t *testing.T, got error, want string) {
18+
assertError := func(t *testing.T, got error, want error) {
1919
t.Helper()
2020
if got == nil {
2121
t.Fatal("wanted an error but didn't get one")
2222
}
2323

24-
if got.Error() != want {
24+
if got != want {
2525
t.Errorf("got %q, want %q", got, want)
2626
}
2727
}
@@ -43,6 +43,6 @@ func TestWallet(t *testing.T) {
4343
err := wallet.Withdraw(Bitcoin(100))
4444

4545
assertBalance(t, wallet, Bitcoin(20))
46-
assertError(t, err, "cannot withdraw, insufficient funds")
46+
assertError(t, err, ErrInsufficientFunds)
4747
})
4848
}

0 commit comments

Comments
 (0)