Skip to content

Commit

Permalink
Update/fix examples
Browse files Browse the repository at this point in the history
  • Loading branch information
spenserblack committed Jun 8, 2022
1 parent 0002951 commit 2fd66fa
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (
"github.com/spenserblack/go-defaultmap"
)

m := defaultmap.NewMap(func() string { return "I'm the default!" })
m := defaultmap.NewMap[string](func() string { return "I'm the default!" })
m.Insert("exists", "Hello, World!")

fmt.Println(m.Get("exists")) // Hello, World!
fmt.Println(m.Get("doesn't exist")) // I'm the default!
fmt.Println(m.GetOr("I'm a one-time default!")) // I'm a one-time default!
fmt.Println(m.GetOr("also doesn't exist", "I'm a one-time default!")) // I'm a one-time default!
```
61 changes: 61 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package defaultmap_test

import (
"fmt"

"github.com/spenserblack/go-defaultmap"
)

func ExampleMap() {
m := defaultmap.NewMap[string](func() string { return "I'm the default!" })
m.Insert("exists", "Hello, World!")

fmt.Println(m.Get("exists"))
fmt.Println(m.Get("doesn't exist"))
fmt.Println(m.GetOr("also doesn't exist", "I'm a one-time default!"))
// Output:
// Hello, World!
// I'm the default!
// I'm a one-time default!
}

func ExampleMap_Get() {
m := defaultmap.NewMap[string](func() string { return "I'm the default!" })
m.Insert("exists", "Hello, World!")

fmt.Println(m.Get("exists"))
fmt.Println(m.Contains("doesn't exist"))
fmt.Println(m.Get("doesn't exist"))
fmt.Println(m.Contains("doesn't exist"))
// Output:
// Hello, World!
// false
// I'm the default!
// true
}

func ExampleMap_GetOr() {
m := defaultmap.NewMap[string](func() string { return "I'm the default!" })
m.Insert("exists", "Hello, World!")

fmt.Println(m.GetOr("exists", "other default"))
fmt.Println(m.Contains("doesn't exist"))
fmt.Println(m.GetOr("doesn't exist", "other default"))
fmt.Println(m.Contains("doesn't exist"))
// Output:
// Hello, World!
// false
// other default
// false
}

func ExampleMap_Contains() {
m := defaultmap.NewMap[string](func() string { return "I'm the default!" })
m.Insert("exists", "Hello, World!")

fmt.Println(m.Contains("exists"))
fmt.Println(m.Contains("doesn't exist"))
// Output:
// true
// false
}

0 comments on commit 2fd66fa

Please sign in to comment.