mirror of
https://github.com/cowardmrx/maps.git
synced 2025-02-22 11:50:38 +08:00
🎉 maps
This commit is contained in:
parent
413948f322
commit
1c7a9de7c7
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
9
.idea/maps.iml
generated
Normal file
9
.idea/maps.iml
generated
Normal file
@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="Go" enabled="true" />
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
4
.idea/misc.xml
generated
Normal file
4
.idea/misc.xml
generated
Normal file
@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="GOROOT" url="file://E:/golang/golang" />
|
||||
</project>
|
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/maps.iml" filepath="$PROJECT_DIR$/.idea/maps.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
8
.idea/watcherTasks.xml
generated
Normal file
8
.idea/watcherTasks.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectTasksOptions">
|
||||
<enabled-global>
|
||||
<option value="go fmt" />
|
||||
</enabled-global>
|
||||
</component>
|
||||
</project>
|
35
README.md
35
README.md
@ -1,4 +1,35 @@
|
||||
# maps
|
||||
|
||||
golang maps
|
||||
可对map根据key进行排序
|
||||
###maps
|
||||
可根据map的key按照有序排序的map包
|
||||
|
||||
###使用:
|
||||
- go get -u github.com/cowardmrx/maps
|
||||
```go
|
||||
// Set
|
||||
maps := NewMaps()
|
||||
maps.Set("key1", "123123")
|
||||
|
||||
// Exist
|
||||
maps.Exist("key1")
|
||||
|
||||
// Get
|
||||
maps.Get("key")
|
||||
|
||||
// Delete
|
||||
maps.Delete("key1","key2")
|
||||
|
||||
// GetMapsKeys return a []string
|
||||
maps.Keys()
|
||||
|
||||
// GetMapsValues return a []interface
|
||||
maps.Values()
|
||||
|
||||
// GetMapsOriginMap get this maps origin data return a map[string]interface
|
||||
maps.OriginMap()
|
||||
|
||||
// SortKey sort this maps by keys
|
||||
maps.SortKey()
|
||||
|
||||
// other example ./maps_test.go
|
||||
```
|
171
maps.go
Normal file
171
maps.go
Normal file
@ -0,0 +1,171 @@
|
||||
package maps
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const (
|
||||
// SortAsc sort by asc
|
||||
SortAsc = "asc"
|
||||
// SortDesc sort by desc
|
||||
SortDesc = "desc"
|
||||
)
|
||||
|
||||
type maps struct {
|
||||
rw sync.RWMutex
|
||||
Key []string
|
||||
Value []interface{}
|
||||
Map map[string]interface{}
|
||||
}
|
||||
|
||||
func NewMaps() Maps {
|
||||
return &maps{
|
||||
Key: []string{},
|
||||
Value: []interface{}{},
|
||||
Map: map[string]interface{}{},
|
||||
}
|
||||
}
|
||||
|
||||
type Maps interface {
|
||||
// Set 设置键值对
|
||||
Set(key string, value interface{}) Maps
|
||||
// Exist 指定的key是否存在
|
||||
Exist(Key string) bool
|
||||
// Get 获取指定Key的值
|
||||
Get(key string) interface{}
|
||||
// Delete 删除指定的某一些key 返回受影响的数目
|
||||
Delete(keys ...string) int
|
||||
// Keys 获取这个map的全部key
|
||||
Keys() []string
|
||||
// Values 获取这个map的全部值
|
||||
Values() []interface{}
|
||||
// OriginMap 获取这个map的原始map
|
||||
OriginMap() map[string]interface{}
|
||||
// SortKey 根据Key排序
|
||||
SortKey(sortFunc func(keys []string))
|
||||
// String 数据转为String
|
||||
String() string
|
||||
}
|
||||
|
||||
// @method Set
|
||||
// @description: set
|
||||
// @receiver m
|
||||
// @param key string
|
||||
// @param value interface{}
|
||||
// @return Maps
|
||||
func (m *maps) Set(key string, value interface{}) Maps {
|
||||
defer m.rw.Unlock()
|
||||
m.rw.Lock()
|
||||
|
||||
m.Key = append(m.Key, key)
|
||||
m.Value = append(m.Value, value)
|
||||
|
||||
m.Map[key] = value
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
// @method Exist
|
||||
// @description: 判断某个key是否存在
|
||||
// @receiver m
|
||||
// @param Key string
|
||||
// @return bool
|
||||
func (m *maps) Exist(Key string) bool {
|
||||
defer m.rw.RUnlock()
|
||||
|
||||
m.rw.RLock()
|
||||
|
||||
for _, v := range m.Key {
|
||||
if Key == v {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// @method Get
|
||||
// @description: 获取指定key的数据
|
||||
// @receiver m
|
||||
// @param key string
|
||||
// @return interface{}
|
||||
func (m *maps) Get(key string) interface{} {
|
||||
defer m.rw.RUnlock()
|
||||
|
||||
m.rw.RLock()
|
||||
|
||||
if !m.Exist(key) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return m.Map[key]
|
||||
}
|
||||
|
||||
// @method Delete
|
||||
// @description: 删除指定key
|
||||
// @receiver m
|
||||
// @param keys ...string
|
||||
// @return int 删除成功的数量
|
||||
func (m *maps) Delete(keys ...string) int {
|
||||
defer m.rw.Unlock()
|
||||
|
||||
m.rw.Lock()
|
||||
|
||||
if len(keys) <= 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
mCount := len(m.Value)
|
||||
|
||||
for _, v := range keys {
|
||||
delete(m.Map, v)
|
||||
}
|
||||
|
||||
return mCount - len(m.Value)
|
||||
}
|
||||
|
||||
// @method Keys
|
||||
// @description: 获取全部的key
|
||||
// @receiver m
|
||||
// @return []string
|
||||
func (m *maps) Keys() []string {
|
||||
return m.Key
|
||||
}
|
||||
|
||||
// @method Values
|
||||
// @description: 获取全部的value
|
||||
// @receiver m
|
||||
// @return []interface{}
|
||||
func (m *maps) Values() []interface{} {
|
||||
return m.Value
|
||||
}
|
||||
|
||||
// @method OriginMap
|
||||
// @description: 获取原始map
|
||||
// @receiver m
|
||||
// @return map[string]interface{}
|
||||
func (m *maps) OriginMap() map[string]interface{} {
|
||||
return m.Map
|
||||
}
|
||||
|
||||
// @method SortKey
|
||||
// @description: 根据Key排序
|
||||
// @receiver m
|
||||
// @param sortFunc func(keys []string)
|
||||
func (m *maps) SortKey(sortFunc func(keys []string)) {
|
||||
sortFunc(m.Key)
|
||||
}
|
||||
|
||||
// @method String
|
||||
// @description: 数据string
|
||||
// @receiver m
|
||||
// @return string
|
||||
func (m *maps) String() string {
|
||||
result, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return string(result)
|
||||
}
|
64
maps_test.go
Normal file
64
maps_test.go
Normal file
@ -0,0 +1,64 @@
|
||||
package maps
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSetMaps(t *testing.T) {
|
||||
maps := NewMaps()
|
||||
a := maps.Set("key1", "123123")
|
||||
fmt.Println(a)
|
||||
}
|
||||
|
||||
func TestExistMaps(t *testing.T) {
|
||||
maps := NewMaps()
|
||||
|
||||
a := maps.Set("key2", "this is value 2")
|
||||
|
||||
fmt.Println(a.Exist("key3"))
|
||||
}
|
||||
|
||||
func TestGetMaps(t *testing.T) {
|
||||
maps := NewMaps()
|
||||
|
||||
a := maps.Set("key1", "value1").Set("key2", "value 2").Set("key3", "value 3")
|
||||
|
||||
fmt.Println(a.Get("key4"))
|
||||
}
|
||||
|
||||
func TestDeleteMaps(t *testing.T) {
|
||||
maps := NewMaps()
|
||||
|
||||
a := maps.Set("key1", "value1").Set("key2", "value 2").Set("key3", "value 3")
|
||||
|
||||
fmt.Println(a.Delete("key1", "key2", "key3", "key4"))
|
||||
}
|
||||
|
||||
func TestKeysValues(t *testing.T) {
|
||||
maps := NewMaps()
|
||||
|
||||
a := maps.Set("key1", "value1").Set("key2", "value 2").Set("key3", "value 3")
|
||||
|
||||
fmt.Println(a.Keys())
|
||||
fmt.Println(a.Values())
|
||||
}
|
||||
|
||||
func TestOriginMap(t *testing.T) {
|
||||
maps := NewMaps()
|
||||
|
||||
a := maps.Set("key1", "value1").Set("key2", "value 2").Set("key3", "value 3")
|
||||
|
||||
fmt.Println(a.OriginMap())
|
||||
}
|
||||
|
||||
func TestSortKey(t *testing.T) {
|
||||
maps := NewMaps()
|
||||
|
||||
a := maps.Set("key3", "value1").Set("key4", "value 2").Set("key1", "value 3")
|
||||
|
||||
a.SortKey(sort.Strings)
|
||||
|
||||
fmt.Println(a)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user