gmap

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 25, 2025 License: MIT Imports: 5 Imported by: 6

README

gmap 包

简介

gmap 包提供了线程安全的 Map 容器,支持保持键的插入顺序,并提供丰富的操作方法。

功能特性

  • 线程安全:所有操作都是线程安全的
  • 有序遍历:保持键的插入顺序
  • 丰富操作:存储、删除、查找、遍历等
  • 内存管理:支持 GC 回收内存

安装

go get github.com/snail007/gmc/util/map

快速开始

package main

import (
    "fmt"
    "github.com/snail007/gmc/util/map"
)

func main() {
    m := gmap.New()
    
    // 存储键值对
    m.Store("name", "John")
    m.Store("age", 30)
    m.Store("city", "New York")
    
    // 获取值
    if value, ok := m.Load("name"); ok {
        fmt.Println("Name:", value)
    }
    
    // 检查是否存在
    if m.Has("age") {
        fmt.Println("Age exists")
    }
    
    // 遍历(按插入顺序)
    m.Range(func(key, value interface{}) bool {
        fmt.Printf("%v: %v\n", key, value)
        return true
    })
    
    // 删除
    m.Delete("city")
    
    // 获取所有键
    keys := m.Keys()
    fmt.Println("Keys:", keys)
    
    // 获取长度
    fmt.Println("Length:", m.Len())
    
    // 转换为普通 map
    normalMap := m.ToMap()
    fmt.Println(normalMap)
}

API 参考

创建和基本操作
  • New() *Map:创建新 Map
  • Store(key, value):存储键值对
  • Load(key) (value, bool):获取值
  • Delete(key):删除键
  • Has(key) bool:检查键是否存在
  • Len() int:获取长度
遍历
  • Range(func(key, value) bool):遍历所有键值对
  • RangeFast(func(key, value) bool):快速遍历(无序)
  • Keys() []interface{}:获取所有键
  • Values() []interface{}:获取所有值
栈和队列操作
  • Shift() (key, value, bool):移除并返回第一个元素
  • Pop() (key, value, bool):移除并返回最后一个元素
其他操作
  • Clear():清空
  • Clone() *Map:克隆
  • ToMap() map[interface{}]interface{}:转换为普通 map
  • GC():垃圾回收,释放内存

类型别名

// M 是 map[string]interface{} 的别名
type M = map[string]interface{}

// Mii 是 map[interface{}]interface{} 的别名
type Mii = map[interface{}]interface{}

// Mss 是 map[string]string 的别名
type Mss = map[string]string

使用场景

  1. 配置管理:存储应用配置
  2. 缓存:内存缓存
  3. 会话存储:HTTP 会话数据
  4. 有序字典:需要保持插入顺序的场景
  5. 并发访问:多 goroutine 安全访问

注意事项

  1. 遍历时按插入顺序
  2. RangeFast 不保证顺序但性能更好
  3. GC() 可以释放已删除键占用的内存
  4. 键可以是任意类型

相关链接

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Less

func Less(v1, v2 interface{}) bool

func Sort

func Sort(s []map[string]interface{}, key string, aes bool)

func SortStr

func SortStr(s []map[string]string, key string, aes bool)

func ToAny

func ToAny(a map[string]string) (b map[string]interface{})

ToAny converts map[string]string to map[string]interface{}

func ToString

func ToString(a map[string]interface{}) (b map[string]string)

ToString converts map[string]interface{} to map[string]string

Types

type M

type M = map[string]interface{}

M alias of type map[string]interface{}

type Map

type Map struct {
	// contains filtered or unexported fields
}

Map a map can kept the sequence of keys when range the map have more useful function, Len(), Shift(), Pop(),Keys(), etc.

func New

func New() *Map

New creates a Map object.

func SortMap

func SortMap(m map[string]interface{}, aes bool) *Map

func SortMapStr

func SortMapStr(m map[string]string, aes bool) *Map

func (*Map) Clear

func (s *Map) Clear()

Clear deletes all data in the map s.

func (*Map) Clone

func (s *Map) Clone() *Map

Clone duplicates the map s.

func (*Map) CloneAndClear

func (s *Map) CloneAndClear() *Map

CloneAndClear duplicates the map s.

func (*Map) Delete

func (s *Map) Delete(key interface{})

Delete deletes the value for a key.

func (*Map) GC

func (s *Map) GC()

GC rebuild the internal map to release memory used by the map.

func (*Map) IndexOf

func (s *Map) IndexOf(k interface{}) int

IndexOf indicates the index of value in Map s, if not found returns -1.

idx start with 0.

func (*Map) IsEmpty

func (s *Map) IsEmpty() bool

IsEmpty indicates if the map is empty.

func (*Map) Keys

func (s *Map) Keys() (keys []interface{})

Keys returns all keys in map s and keep the sequence of store sequence.

func (*Map) Len

func (s *Map) Len() int

Len returns the length of the map s.

func (*Map) Load

func (s *Map) Load(key interface{}) (value interface{}, ok bool)

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.

func (*Map) LoadAndDelete

func (s *Map) LoadAndDelete(key interface{}) (value interface{}, loaded bool)

LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present.

func (*Map) LoadAndStoreFunc

func (s *Map) LoadAndStoreFunc(key interface{}, f func(oldValue interface{}, loaded bool) (newValue interface{})) (newValue interface{}, loaded bool)

LoadAndStoreFunc call the given func and stores it returns value. The loaded result is true if the keys was exists, false if not exists. If loaded, the given func firstly parameter is the loaded value, otherwise is nil.

func (*Map) LoadAndStoreFuncErr

func (s *Map) LoadAndStoreFuncErr(key interface{}, f func(oldValue interface{}, loaded bool) (newValue interface{}, err error)) (newValue interface{}, loaded bool, err error)

LoadAndStoreFuncErr call the given func and stores it returns value, if func returns an error, the error be returned. The loaded result is true if the keys was exists, false if not exists. If loaded, the given func firstly parameter is the loaded value, otherwise is nil.

func (*Map) LoadOrStore

func (s *Map) LoadOrStore(key, value interface{}) (actual interface{}, loaded bool)

LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.

func (*Map) LoadOrStoreFront

func (s *Map) LoadOrStoreFront(key, value interface{}) (actual interface{}, loaded bool)

LoadOrStoreFront returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored. The key will be stored the first in keys queue if key not exists.

func (*Map) LoadOrStoreFunc

func (s *Map) LoadOrStoreFunc(key interface{}, f func() interface{}) (actual interface{}, loaded bool)

LoadOrStoreFunc returns the existing value for the key if present. Otherwise, it call the given func and stores it returns value. The loaded result is true if the value was loaded, false if stored.

func (*Map) LoadOrStoreFuncErr

func (s *Map) LoadOrStoreFuncErr(key interface{}, f func() (x interface{}, err error)) (actual interface{}, loaded bool, err error)

LoadOrStoreFuncErr returns the existing value for the key if present. Otherwise, it call the given func and stores it returns value, if the func returns an error, nothing will be stored, the function's error be returned. The loaded result is true if the value was loaded, false if stored.

func (*Map) Merge

func (s *Map) Merge(m *Map) *Map

Merge merges a Map to Map s.

func (*Map) MergeMap

func (s *Map) MergeMap(m Mii) *Map

MergeMap merges a map to Map s.

func (*Map) MergeStrMap

func (s *Map) MergeStrMap(m M) *Map

MergeStrMap merges a map to Map s.

func (*Map) MergeStrStrMap

func (s *Map) MergeStrStrMap(m Mss) *Map

MergeStrStrMap merges a map to Map s.

func (*Map) MergeSyncMap

func (s *Map) MergeSyncMap(m *sync.Map) *Map

MergeSyncMap merges a sync.Map to Map s.

func (*Map) Pop

func (s *Map) Pop() (k, v interface{}, ok bool)

Pop returns the last element of map s or nil if the map is empty.

func (*Map) Range

func (s *Map) Range(f func(key, value interface{}) bool)

Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.

Range keep the sequence of store sequence.

func (*Map) RangeFast

func (s *Map) RangeFast(f func(key, value interface{}) bool)

RangeFast calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.

RangeFast keep the sequence of store sequence.

RangeFast do not create a snapshot for range, so you can not modify map s in range loop, indicate do not call Delete(), Store(), LoadOrStore(), Merge(), etc.

func (*Map) Shift

func (s *Map) Shift() (k, v interface{}, ok bool)

Shift returns the first element of map s or nil if the map is empty.

func (*Map) Store

func (s *Map) Store(key, value interface{})

Store sets the value for a key.

func (*Map) StoreFront

func (s *Map) StoreFront(key, value interface{})

StoreFront sets the value for a key. The key will be stored the first in keys queue.

func (*Map) String

func (s *Map) String() string

String returns string format of the Set.

func (*Map) StringKeys

func (s *Map) StringKeys() (keys []string)

StringKeys returns all keys in map s and keep the sequence of store sequence.

func (*Map) ToMap

func (s *Map) ToMap() map[interface{}]interface{}

ToMap duplicates the map s.

func (*Map) ToStringMap

func (s *Map) ToStringMap() map[string]interface{}

ToStringMap duplicates the map s.

type Mii

type Mii = map[interface{}]interface{}

Mii alias of type map[interface{}]interface{}

type Mss

type Mss = map[string]string

Mss alias of type map[string]string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL