Skip to content

Commit

Permalink
Implement GetOrDefault (#8)
Browse files Browse the repository at this point in the history
GetOrDefault returns the value for a key. If the key does not exist, it returns the default value instead.
  • Loading branch information
Kaitian Xie authored and elliotchance committed Nov 18, 2019
1 parent 1a5aef6 commit 63846df
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions orderedmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ func (m *OrderedMap) Set(key, value interface{}) bool {
return !didExist
}

// GetOrDefault returns the value for a key. If the key does not exist, returns
// the default value instead.
func (m *OrderedMap) GetOrDefault(key, defaultValue interface{}) interface{} {
if value, ok := m.kv[key]; ok {
return value.Value.(*orderedMapElement).value
}

return defaultValue
}

// Len returns the number of elements in the map.
func (m *OrderedMap) Len() int {
return len(m.kv)
Expand Down

0 comments on commit 63846df

Please sign in to comment.