this repo has no description
1package pointers
2
3import (
4 "fmt"
5 "errors"
6)
7
8type Bitcoin int
9
10type Stringer interface {
11 String() string
12}
13
14func (b Bitcoin) String() string {
15 return fmt.Sprintf("%d BTC", b)
16}
17
18type Wallet struct {
19 // when using lowercase properties names, it will be private
20 balance Bitcoin
21}
22
23func (w *Wallet) Deposit(amount Bitcoin) {
24 w.balance += amount
25}
26
27func (w *Wallet) Balance() Bitcoin {
28 return w.balance
29}
30
31func (w *Wallet) Withdraw(amount Bitcoin) error {
32 if amount > w.balance {
33 return errors.New("cannot withdraw, insufficient funds")
34 }
35
36 w.balance -= amount
37 return nil
38}