Load returns the value stored in the map for a key, or nil if no value is present. The ok result indicates whether value was found in the map.
(key K)
| 101 | // value is present. |
| 102 | // The ok result indicates whether value was found in the map. |
| 103 | func (m *MapOf[K, V]) Load(key K) (value V, ok bool) { |
| 104 | read, _ := m.read.Load().(readOnly[K, V]) |
| 105 | e, ok := read.m[key] |
| 106 | if !ok && read.amended { |
| 107 | m.mu.Lock() |
| 108 | // Avoid reporting a spurious miss if m.dirty got promoted while we were |
| 109 | // blocked on m.mu. (If further loads of the same key will not miss, it's |
| 110 | // not worth copying the dirty map for this key.) |
| 111 | read, _ = m.read.Load().(readOnly[K, V]) |
| 112 | e, ok = read.m[key] |
| 113 | if !ok && read.amended { |
| 114 | e, ok = m.dirty[key] |
| 115 | // Regardless of whether the entry was present, record a miss: this key |
| 116 | // will take the slow path until the dirty map is promoted to the read |
| 117 | // map. |
| 118 | m.missLocked() |
| 119 | } |
| 120 | m.mu.Unlock() |
| 121 | } |
| 122 | if !ok { |
| 123 | return value, false |
| 124 | } |
| 125 | return e.load() |
| 126 | } |
| 127 | |
| 128 | func (m *MapOf[K, V]) Has(key K) bool { |
| 129 | _, ok := m.Load(key) |
no test coverage detected