-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0002951
commit 2fd66fa
Showing
2 changed files
with
63 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |