Skip to content

Commit

Permalink
Improve and expand add/sub examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
bojanz committed Jun 29, 2022
1 parent c241c5f commit 97af46f
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,12 @@ func ExampleAmount_Add() {
// Output: 24.49 USD
}

func ExampleAmount_sum() {
var amounts []currency.Amount
for i := 0; i <= 4; i++ {
amount, err := currency.NewAmount(strconv.Itoa(i), "AUD")
if err != nil {
panic(err)
}
amounts = append(amounts, amount)
}

// It is OK to add any currency.Amount to the zero value.
func ExampleAmount_Add_sum() {
// Any currency.Amount can be added to the zero value.
var sum currency.Amount
for _, a := range amounts {
var err error
sum, err = sum.Add(a)
if err != nil {
panic(err)
}
for i := 0; i <= 4; i++ {
a, _ := currency.NewAmount(strconv.Itoa(i), "AUD")
sum, _ = sum.Add(a)
}

fmt.Println(sum) // 0 + 1 + 2 + 3 + 4 = 10
Expand All @@ -92,6 +80,18 @@ func ExampleAmount_Sub() {
// Output: 15.99 USD
}

func ExampleAmount_Sub_diff() {
// Any currency.Amount can be subtracted from the zero value.
var diff currency.Amount
for i := 0; i <= 4; i++ {
a, _ := currency.NewAmount(strconv.Itoa(i), "AUD")
diff, _ = diff.Sub(a)
}

fmt.Println(diff) // 0 - 1 - 2 - 3 - 4 = -10
// Output: -10 AUD
}

func ExampleAmount_Mul() {
amount, _ := currency.NewAmount("20.99", "USD")
taxAmount, _ := amount.Mul("0.20")
Expand Down

0 comments on commit 97af46f

Please sign in to comment.