支持Map类型的包装 #6

Merged
zhangdeman merged 9 commits from feature/support_map into master 2024-11-19 16:31:05 +08:00
2 changed files with 67 additions and 0 deletions
Showing only changes of commit 5ea67a5b6c - Show all commits

46
map.go Normal file
View File

@ -0,0 +1,46 @@
// Package wrapper ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-11-06 18:27
package wrapper
import "sync"
var mapLock = &sync.RWMutex{}
type Map map[string]any
func (m *Map) lock() {
mapLock.Lock()
}
func (m *Map) unlock() {
mapLock.Unlock()
}
func (m *Map) rlock() {
mapLock.RLock()
}
func (m *Map) rUnlock() {
mapLock.RUnlock()
}
// Exist key是否存在
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:34 2024/11/6
func (m *Map) Exist(key string) bool {
if nil == m {
return false
}
m.rlock()
defer m.unlock()
v := *m
_, exist := v[key]
return exist
}

21
map_test.go Normal file
View File

@ -0,0 +1,21 @@
// Package wrapper ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-11-06 18:37
package wrapper
import (
"fmt"
"testing"
)
func TestMap_Exist(t *testing.T) {
testData := Map(map[string]any{
"name": "zhang",
})
fmt.Println(testData.Exist("name"))
fmt.Println(testData.Exist("age"))
}