mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-05 11:37:46 +00:00
sync: implement sync.Swap
This commit is contained in:
committed by
Ron Evans
parent
e368551919
commit
d6343d9d5c
@@ -70,3 +70,15 @@ func (m *Map) Range(f func(key, value interface{}) bool) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Swap replaces the value for the given key, and returns the old value if any.
|
||||
func (m *Map) Swap(key, value any) (previous any, loaded bool) {
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
if m.m == nil {
|
||||
m.m = make(map[interface{}]interface{})
|
||||
}
|
||||
previous, loaded = m.m[key]
|
||||
m.m[key] = value
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user