Unlike an unordered map, the orderedmap package is an ordermap type. Ordermap is an inherited Map maps keys to values. Ordermap provides useful methods e.g. Store, Load, Delete , Range and so on. The remaining methods are order-aware. Big-O running times for all methods are the same as regular maps.
Get source code by go get command:
go get cnb.cool/ordermap/ordermap@v1.1.0
Or you can write following code in go.mod for your project:
require ( cnb.cool/ordermap/ordermap v1.1.0 )
func New() *OrderMap
For example:
package main
import (
"fmt"
"cnb.cool/ordermap/ordermap"
)
func main() {
o := ordermap.New()
fmt.Printf("%T", o)
// output: *ordermap.OrderMap
}
Store method used to set the value for a key.
func (om *OrderMap) Store(key, value any)
For example:
package main
import (
"fmt"
"cnb.cool/ordermap/ordermap"
)
func main() {
o := ordermap.New()
o.Store("name", "Pizza")
o.Store("price", 50)
o.Store("size", "10#")
fmt.Println(o)
// output: {"name": "Pizza", "price": 50, "size": "10#"}
}
Load method used to get the value from a key. If the key is not exist in the map, a nil and a false will be returned.
func (om *OrderMap) Load(key any) (any, bool)
For example:
package main
import (
"fmt"
"cnb.cool/ordermap/ordermap"
)
func main() {
o := ordermap.New()
o.Store("name", "Pizza")
o.Store("price", 50)
o.Store("size", "10#")
size, ok := o.Load("size")
fmt.Println(size, ok)
// output: 10# true
color, ok := o.Load("color")
fmt.Println(color, ok)
// output: <nil> false
}
Delete method is used to delete the value associated with a key.
func (om *OrderMap) Delete(key any)
For example:
package main
import (
"fmt"
"cnb.cool/ordermap/ordermap"
)
func main() {
o := ordermap.New()
o.Store("name", "Pizza")
o.Store("price", 50)
o.Store("size", "10#")
fmt.Println("Before using delete method, the content of ordermap is: ", o)
// Before using delete method, the content of ordermap is: {"name": "Pizza", "price": 50, "size": "10#"}
o.Delete("size")
fmt.Println("After using delete method, the content of ordermap is: ", o)
// After using delete method, the content of ordermap is: {"name": "Pizza", "price": 50}
}
You can use the Range method to visit each key and value. The Range method requires an argument of type func(key, value any) bool. If returns false, the iteration stops.
func (om *OrderMap) Range(f func(key, value any) bool)
For example:
package main
import (
"fmt"
"cnb.cool/ordermap/ordermap"
)
func main() {
o := ordermap.New()
o.Store("A", "a")
o.Store("B", "b")
f := func(key, value interface{}) bool {
fmt.Printf("key: %v, value: %v\n", key, value)
return true
}
o.Range(f)
// outputs:
// key: A, value: a
// key: B, value: b
}
You can use the Length method to obtain the length of the OrderMap.
func (om *OrderMap) Length() int
For example:
package main
import (
"fmt"
"cnb.cool/ordermap/ordermap"
)
func main() {
o := ordermap.New()
o.Store("A", "a")
o.Store("B", "b")
fmt.Println(o.Length())
// output: 2
}
OrderMap also implements some common interfaces, which will be more convenient when actually using it.
package main
import (
"fmt"
"cnb.cool/ordermap/ordermap"
)
func main() {
o := ordermap.New()
o.Store("A", "a")
o.Store("B", "b")
o.Store(1, 3.14)
fmt.Println(o)
// {"A": "a", "B": "b", 1: 3.14}
fmt.Printf("The content of OrderMap object is: %s\n", o)
// The content of OrderMap object is: {"A": "a", "B": "b", 1: 3.14}
}
package main
import (
"fmt"
"cnb.cool/ordermap/ordermap"
)
func main() {
o := ordermap.New()
o.Store("A", "a")
o.Store(1, 3.14)
fmt.Println(o)
// {"A": "a", 1: 3.14}
fmt.Printf("The content of OrderMap object is: %#v\n", o)
// The content of OrderMap object is: {"A": "a", 1: 3.14}
}