-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add generic example of ordered map
- Loading branch information
Showing
1 changed file
with
94 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
//go:build go1.21 | ||
// +build go1.21 | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/fufuok/utils/orderedmap" | ||
) | ||
|
||
func main() { | ||
om := orderedmap.NewOf[int]() | ||
om.Set("a", 1) | ||
om.Set("b", 2) | ||
om.Sort(func(a *orderedmap.PairOf[int], b *orderedmap.PairOf[int]) bool { | ||
return a.Value() > b.Value() | ||
}) | ||
for _, k := range om.Keys() { | ||
v := om.MustGet(k) | ||
fmt.Println(k, v) | ||
} | ||
|
||
c := om.Clone() | ||
m := om.ToMap() | ||
_ = c | ||
_ = m | ||
|
||
// use NewOf[any]() instead of o := map[string]any{} | ||
o := orderedmap.NewOf[any]() | ||
|
||
// use Set instead of o["a"] = 1 | ||
o.Set("a", 1) | ||
o.Set("c", 2) | ||
|
||
// add some value with special characters | ||
o.Set("b", "\\.<>[]{}_-") | ||
|
||
// use Get instead of i, ok := o["a"] | ||
val, ok := o.Get("a") | ||
fmt.Println("Get:", val, ok) | ||
|
||
// use Keys instead of for k, v := range o | ||
keys := o.Keys() | ||
for _, k := range keys { | ||
v, _ := o.Get(k) | ||
fmt.Println(k, v) | ||
} | ||
|
||
// use o.Delete instead of delete(o, key) | ||
o.Delete("b") | ||
|
||
fmt.Println("sort the keys:") | ||
o.SortKeys() | ||
for _, k := range o.Keys() { | ||
v, _ := o.Get(k) | ||
fmt.Println(k, v) | ||
} | ||
|
||
fmt.Println("sort by Pair:") | ||
o.Sort(func(a *orderedmap.PairOf[any], b *orderedmap.PairOf[any]) bool { | ||
return a.Value().(int) < b.Value().(int) | ||
}) | ||
for _, k := range o.Keys() { | ||
v, _ := o.Get(k) | ||
fmt.Println(k, v) | ||
} | ||
|
||
fmt.Println("sort by Pair(reverse):") | ||
o.Sort(func(a *orderedmap.PairOf[any], b *orderedmap.PairOf[any]) bool { | ||
return a.Value().(int) > b.Value().(int) | ||
}) | ||
for _, k := range o.Keys() { | ||
v, _ := o.Get(k) | ||
fmt.Println(k, v) | ||
} | ||
} | ||
|
||
// Output: | ||
// b 2 | ||
// a 1 | ||
// Get: 1 true | ||
// a 1 | ||
// c 2 | ||
// b \.<>[]{}_- | ||
// sort the keys: | ||
// a 1 | ||
// c 2 | ||
// sort by Pair: | ||
// a 1 | ||
// c 2 | ||
// sort by Pair(reverse): | ||
// c 2 | ||
// a 1 |