File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -152,3 +152,28 @@ m["b"] = "2"
152152m[" c" ] = " 3"
153153get := mapex.Values (m)
154154```
155+ ## 十一、` rwmap `
156+ rwmap与sync.Map类似支持并发访问,只解决sync.Map 2个问题.
157+ 1 . 没有Len成员函数
158+ 2 . 以及没有使用泛型语法,有运行才发现类型使用错误的烦恼
159+ ``` go
160+ var m RWMap [string , string ] // 声明一个string, string的map
161+ m.Store (" hello" , " 1" ) // 保存
162+ v1 , ok1 := m.Load (" hello" ) // 获取值
163+ v1, ok1 = m.LoadAndDelete (" hello" ) // 返回hello对应值,然后删除hello
164+ Delete (" hello" ) // 删除
165+ v1, ok1 = m.LoadOrStore (" hello" , " world" )
166+
167+ // 遍历,使用回调函数
168+ m.Range (func (key, val string ) bool {
169+ fmt.Printf (" k:%s , val:%s \n " i, key, val)
170+ return true
171+ })
172+
173+ // 遍历,迭代器
174+ for pair := range m.Iter () {
175+ fmt.Printf (" k:%s , val:%s \n " , pair.Key , pair.Val )
176+ }
177+
178+ m.Len ()// 获取长度
179+ ```
You can’t perform that action at this time.
0 commit comments