Ordered Map
$ go get github.com/theterminalguy/om
New creates a new ordered map
m := om.New()
Add adds a key value pair to the map. If the key already exist, it's value is updated
m := om.New()
m.Add("first_name", "Simon Peter")
m.Add("last_name", "Damian")
m.Add("age", 27)
m.Add("sex", "male")
m.Add("married", false)
m.Add("age", 28) // updates the age to 28 because key exists
Updates a key's value using Put. If the key is not found, it calls Add
m := om.New()
m.Set("first_name", "Simon Peter") // calls Add
Updates a key's value. If the key is not found, it returns an error
m := om.New()
m.Put("first_name", "Simon Peter") // returns an error
m.Add("first_name", "Simon Peter")
m.Put("first_name", "Simon") // updates the value of first_name to Simon without error
Returns the value of a key. If the key is not found, it returns an error
m := om.New()
m.Add("first_name", "Simon Peter")
v, err := m.Get("first_name")
if err != nil {
panic(err)
}
fmt.Println(v) // prints Simon Peter
Returns the value of a key. If the key is not found, it returnns the default value
m := om.New()
v := m.Fetch("first_name", "Simon Peter")
fmt.Println(v) // prints Simon Peter
m.Add("last_name", "Damian")
v = m.Fetch("last_name", "Doe")
fmt.Println(v) // prints Damian
Index return the key/value pair stored at the given index. If the index is out of bound, returns an empty string and
nil
m := om.New()
m.Add("first_name", "Simon Peter")
m.Add("last_name", "Damian")
k, v := m.Index(0)
fmt.Println(k, v) // prints first_name Simon Peter
k, v = m.Index(2)
fmt.Println(k, v) // prints last_name Damian
k, v = m.Index(3)
fmt.Println(k, v) // prints (empty string) nil
Returns the numeric index for the given key. If the key is not found, it returns -1
m := om.New()
m.Add("first_name", "Simon Peter")
i := m.GetKeyIndex("first_name")
fmt.Println(i) // prints 0
i = m.GetKeyIndex("last_name")
fmt.Println(i) // prints -1
Returns a new slice containing the values of the given keys
m := om.New()
m.Add("first_name", "Simon Peter")
m.Add("last_name", "Damian")
v := m.ValuesAt("first_name", "last_name")
fmt.Println(v) // prints [Simon Peter Damian]
Returns true if the key is contained in the map
m := om.New()
m.Add("first_name", "Simon Peter")
fmt.Println(m.HasKey("first_name")) // prints true
fmt.Println(m.HasKey("last_name")) // prints false
Returns true if any of the key/value pair satisfies the given callback
m := om.New()
m.Add("first_name", "Simon Peter")
m.HasAny(func(key string, value interface{}) bool {
return key == "first_name" && value == "Simon Peter"
}) // prints true
m.HasAny(func(key string, value interface{}) bool {
return key == "last_name" && value == "Damian"
}) // prints false
Removes the entry with the given key from the map. If the key is not found, it returns an error
m := om.New()
m.Add("first_name", "Simon Peter")
v, err := m.Delete("first_name")
if err != nil {
panic(err)
}
fmt.Println(v) // prints Simon Peter
Calls the given callback for each key/value pair in the map. If the callback returns true, the key/value pair is removed from the map
m := om.New()
m.Add("first_name", "Simon Peter")
m.DeleteIF(func(key string, value interface{}) bool {
return key == "first_name" && value == "Simon Peter"
})
fmt.Println(m.HasKey("first_name")) // prints false
Calls the callback with each key/value pair. Keeps each entry for which the callback returns true, otherwise deletes the entry from the map
m := om.New()
m.Add("first_name", "Simon Peter")
m.Add("last_name", "Damian")
m.Add("age", 27)
m.KeepIF(func(key string, value interface{}) bool {
return key == "first_name" && value == "Simon Peter"
})
fmt.Println(m.HasKey("first_name")) // prints true
fmt.Println(m.HasKey("last_name")) // prints false
fmt.Println(m.HasKey("age")) // prints false
Returns a new map containing the entries for which the callback returns true
m := om.New()
m.Add("fish", "tuna")
m.Add("fruit", "apple")
m.Add("vegetable", "carrot")
n := m.Filter(func(key string, value interface{}) bool {
return key == "fish" || value == "apple"
})
fmt.Println(n.HasKey("fish")) // prints true
fmt.Println(n.HasKey("fruit")) // prints true
fmt.Println(n.HasKey("vegetable")) // prints false
Modifies the map to contain only the entries for which the callback returns true
m := om.New()
m.Add("fish", "tuna")
m.Add("fruit", "apple")
m.Add("vegetable", "carrot")
m.Filter_(func(key string, value interface{}) bool {
return key == "fish" || value == "apple"
})
fmt.Println(m.HasKey("fish")) // prints true
fmt.Println(m.HasKey("fruit")) // prints true
fmt.Println(m.HasKey("vegetable")) // prints false
Returns a new map containing the entries for the given keys
m := om.New()
m.Add("fish", "tuna")
m.Add("fruit", "apple")
m.Add("vegetable", "carrot")
n := m.Slice("fish", "fruit")
fmt.Println(n.HasKey("fish")) // prints true
fmt.Println(n.HasKey("fruit")) // prints true
fmt.Println(n.HasKey("vegetable")) // prints false
Returns a new map with all nil values removed
m := om.New()
m.Add("fish", "tuna")
m.Add("fruit", "apple")
m.Add("vegetable", nil)
m.Add("meat", nil)
n := m.Compact()
fmt.Println(n.HasKey("fish")) // prints true
fmt.Println(n.HasKey("fruit")) // prints true
fmt.Println(n.HasKey("vegetable")) // prints false
fmt.Println(n.HasKey("meat")) // prints false
Modifies the original map with all nil values removed
m := om.New()
m.Add("fish", "tuna")
m.Add("fruit", "apple")
m.Add("vegetable", nil)
m.Add("meat", nil)
m.Compact_()
fmt.Println(m.HasKey("fish")) // prints true
fmt.Println(m.HasKey("fruit")) // prints true
fmt.Println(m.HasKey("vegetable")) // prints false
fmt.Println(m.HasKey("meat")) // prints false
Returns a new map excluding entries for the given keys
m := om.New()
m.Add("fish", "tuna")
m.Add("fruit", "apple")
m.Add("vegetable", "carrot")
n := m.Except("fish", "fruit")
fmt.Println(n.HasKey("fish")) // prints false
fmt.Println(n.HasKey("fruit")) // prints false
fmt.Println(n.HasKey("vegetable")) // prints true
Returns a new map formed by merging the given map with the original map
m := om.New()
m.Add("fish", "tuna")
m.Add("fruit", "apple")
n := m.Merge(om.New().Add("fruit", "banana"))
fmt.Println(n.HasKey("fish")) // prints true
fmt.Println(n.HasKey("fruit")) // prints true
fmt.Println(n.Get("fruit")) // prints banana
fmt.Println(m.HasKey("fruit")) // prints true
fmt.Println(m.Get("fruit")) // prints apple
Modifies the original map by mergin the given map with the original map
m := om.New()
m.Add("fish", "tuna")
m.Add("fruit", "apple")
m.Merge_(om.New().Add("fruit", "banana"))
fmt.Println(m.HasKey("fish")) // prints true
fmt.Println(m.HasKey("fruit")) // prints true
fmt.Println(m.Get("fruit")) // prints banana
Removes all entries from the map
m := om.New()
m.Add("fish", "tuna")
m.Add("fruit", "apple")
m.Clear()
fmt.Println(m.HasKey("fish")) // prints false
fmt.Println(m.HasKey("fruit")) // prints false
Returns a slice of all keys in the map
m := om.New()
m.Add("fish", "tuna")
m.Add("fruit", "apple")
m.Add("vegetable", "carrot")
fmt.Println(m.Keys()) // prints [fish fruit vegetable]
Returns a slice of all keys in the map in reverse order
m := om.New()
m.Add("fish", "tuna")
m.Add("fruit", "apple")
m.Add("vegetable", "carrot")
fmt.Println(m.RKeys()) // prints [vegetable fruit fish]
Returns a slice of all values in the map
m := om.New()
m.Add("fish", "tuna")
m.Add("fruit", "apple")
m.Add("vegetable", "carrot")
fmt.Println(m.Values()) // prints [tuna apple carrot]
Returns a slice of all values in the map in reverse order
m := om.New()
m.Add("fish", "tuna")
m.Add("fruit", "apple")
m.Add("vegetable", "carrot")
fmt.Println(m.RValues()) // prints [carrot apple tuna]
Compares two maps for equality. A map is equal to another map if they have the same keys and values.
m := om.New()
m.Add("fish", "tuna")
m.Add("fruit", "apple")
n := om.New()
n.Add("fish", "tuna")
n.Add("fruit", "apple")
o := om.New()
o.Add("fish", "tuna")
o.Add("fruit", "banana")
fmt.Println(m.EQ(n)) // prints true
fmt.Println(m.EQ(o)) // prints false
Checks if two maps have the same keys. The values are not checked.
m := om.New()
m.Add("fish", "tuna")
m.Add("fruit", "apple")
n := om.New()
n.Add("fish", "salmon")
n.Add("fruit", "banana")
Iterates over the map and calls the callback function for each entry
m := om.New()
m.Add("fish", "tuna")
m.Add("fruit", "apple")
m.Each(func(key string, value interface{}) {
fmt.Println(key, value)
})
// prints
// fish tuna
// fruit apple
Iterates over the map in reverse order and calls the callback function for each entry
m := om.New()
m.Add("fish", "tuna")
m.Add("fruit", "apple")
m.REach(func(key string, value interface{}) {
fmt.Println(key, value)
})
// prints
// fruit apple
// fish tuna
Returns the number of entries in the map
m := om.New()
m.Add("fish", "tuna")
m.Add("fruit", "apple")
fmt.Println(m.Size()) // prints 2
Checks if the map is empty
m := om.New()
fmt.Println(m.IsEmpty()) // prints true
Returns the underyling go map
m := om.New()
m.Add("fish", "tuna")
m.Add("fruit", "apple")
fmt.Println(m.OM()) // prints map[fish:tuna fruit:apple]
Returns the map as a JSON string
m := om.New()
m.Add("fish", "tuna")
m.Add("fruit", "apple")
fmt.Println(m.JSON()) // prints {"fish":"tuna","fruit":"apple"}
Joins the map into a string. The glue is used to separate the entries. The lpad and rpad are used to pad the key and value.
m := om.New()
m.Add("fish", "tuna")
m.Add("fruit", "apple")
fmt.Println(m.Join(" ", "(", ")")) // prints (fish tuna) (fruit apple)
Joins the map into a string in reverse order. The glue is used to separate the entries. The lpad and rpad are used to pad the key and value.
m := om.New()
m.Add("fish", "tuna")
m.Add("fruit", "apple")
fmt.Println(m.RJoin(" ", "(", ")")) // prints (fruit apple) (fish tuna)
Returns a string containing the map entries in the format
key=value
m := om.New()
m.Add("fish", "tuna")
m.Add("fruit", "apple")
fmt.Println(m.String()) // prints fish=tuna fruit=apple