From 5ea67a5b6c594e8eef3a5be390e6ed599ba2b955 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 6 Nov 2024 18:40:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90map=E5=8C=85=E8=A3=85?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=20-=20key=E6=98=AF=E5=90=A6=E5=AD=98?= =?UTF-8?q?=E5=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- map.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ map_test.go | 21 +++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 map.go create mode 100644 map_test.go diff --git a/map.go b/map.go new file mode 100644 index 0000000..6d23965 --- /dev/null +++ b/map.go @@ -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 +} diff --git a/map_test.go b/map_test.go new file mode 100644 index 0000000..b5e022e --- /dev/null +++ b/map_test.go @@ -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")) +}