Standard library / sync

sync.Map.CompareAndDelete()

Delete the entry only if its current value equals old. The old value must be comparable. Expected stdout: 'false true\n'.

Formal Syntax & Structure

go syntaxgo
func (m *Map) CompareAndDelete(key, old any) (deleted bool)

Parameters & Attributes

NameTypeDescription
keyanyComparable map key, or a package-specific comparable context key.
oldanyExpected current value for comparison.

Return Value

(deleted bool)

Whether the matching entry was deleted.

Working Example

Example Snippetgo
package main
import (  "fmt"  "sync")
func main() {  var m sync.Map  m.Store("a", 1)  fmt.Println(m.CompareAndDelete("a", 2), m.CompareAndDelete("a", 1))}
Go Reference