Skip to content

Commit

Permalink
调整:Dictionary.Update 当元素不存在时,不更新
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Oct 22, 2024
1 parent ef839b7 commit 31d4284
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions dictionary.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ func (receiver *Dictionary[TKey, TValue]) Add(key TKey, value TValue) {

// Update 更新元素
func (receiver *Dictionary[TKey, TValue]) Update(key TKey, f func(value *TValue)) {
v, _ := receiver.source.Load(key)
v2 := v.(TValue)
f(&v2)
receiver.source.Store(key, v2)
if v, exists := receiver.source.Load(key); exists {
v2 := v.(TValue)
f(&v2)
receiver.source.Store(key, v2)
}
}

// Clear 清除元素
Expand Down

0 comments on commit 31d4284

Please sign in to comment.