vmap

package module
v2.3.3 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2022 License: Apache-2.0 Imports: 5 Imported by: 10

README

vmap Build Status

golang Map,go Map, 可多线程安全读写,内部封装于 sync.Map。

列表:

type Map struct {}                                       // Map结构
func NewMap() *Map                                          // Map对象
func (m *Map) New(key interface{}) *Map                     // 新建一个key对应的Map对象,并返回该Map
func (m *Map) GetNewMap(key interface{}) *Map               // 如果不存在增加一个key对应的Map对象,否则读取并返回该对象。(一次单个)
func (m *Map) GetNewMaps(keys ...interface{}) *Map          // 如果不存在增加一个key对应的Map对象,否则读取并返回该对象。(一次多个)
func (m *Map) Len() int                                     // 长度
func (m *Map) Set(key, val interface{})                     // 设置
func (m *Map) SetExpired(key interface{}, d time.Duration)  // 设置KEY有效期,过期会自动删除
func (m *Map) SetExpiredCall(key interface{}, d time.Duration, f func(interface{}))    // 设置KEY有效期,过期会自动删除,并调用函数
func (m *Map) Get(key interface{}) interface{}              // 读取
func (m *Map) Has(key interface{}) bool                     // 判断
func (m *Map) GetOrDefault(key interface{}, def interface{}) interface{}        //读取,没有返回默认值
func (m *Map) GetHas(key interface{}) (val interface{},    ok bool)             // 读取并判断
func (m *Map) Index(key ...interface{})    interface{}                          // 快速索引
func (m *Map) IndexHas(key ...interface{}) (interface{}, bool)                  // 快速索引+判断
func (m *Map) Del(key interface{})                           // 删除
func (m *Map) Dels(key []interface{})                        // 批量删除
func (m *Map) ReadAll() interface{}                          // 读取所有
func (m *Map) Range(f func(key, value interface{}) bool)     // 遍历,返回true继续,否则退出
func (m *Map) Keys() []interface{}                           // Map中的所有键名
func (m *Map) Values() []interface{}                         // Map中的所有值
func (m *Map) Reset()                                        // 重置
func (m *Map) Copy(from *Map)                                // 复制,从 from 复制写入到m
func (m *Map) MarshalJSON() ([]byte, error)                  // 编码JSON
func (m *Map) UnmarshalJSON(data []byte) error               // 解码JSON
func (m *Map) WriteTo(mm interface{}) (err error)            // 写入到 mm
func (m *Map) ReadFrom(mm interface{}) error                 // 从 mm 读取
func (m *Map) String() string                                // 字符串

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Map

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

Map 推荐使用NewMap来规范的创建,这样可避免不必要的错误。

func NewMap

func NewMap() *Map

NewMap 对象

*Map      Map对象

func (*Map) Copy

func (T *Map) Copy(from *Map, over bool)

Copy 从 from 复制所有并写入到 m 中

from *Map       Map对象
error           错误

func (*Map) Del

func (T *Map) Del(key any)

Del 删除

key any   键名

func (*Map) Dels

func (T *Map) Dels(keys []any)

Dels 删除

keys []any   键名

func (*Map) Get

func (T *Map) Get(key any) any

Get 读取

key any   读取值的键名
any       读取值

func (*Map) GetHas

func (T *Map) GetHas(key any) (val any, ok bool)

GetHas 读取并判断

key any   键名
val any   读取值
ok bool           判断,如果为true,判断存在。否则为flase

func (*Map) GetNewMap

func (T *Map) GetNewMap(key any) *Map

GetNewMap 如果不存在增加一个key对应的Map对象,否则读取并返回该对象。

key any     键名
*Map                Map对象

func (*Map) GetNewMaps

func (T *Map) GetNewMaps(keys ...any) *Map

GetNewMaps 如果不存在增加一个key对应的Map对象,否则读取并返回该对象。 他支持链式读取或创建,如果你想相独读取,可以使用 .Index 方法

key ...any     键名
*Map                Map对象

func (*Map) GetOrDefault

func (T *Map) GetOrDefault(key any, def any) any

GetOrDefault 读取,如果不存,返回默认值

key any   	键名
def any		默认值
val any   	读取值

func (*Map) Has

func (T *Map) Has(key any) bool

Has 判断

key any   键名
bool              判断,如果为true,判断存在。否则为flase

func (*Map) Index

func (T *Map) Index(key ...any) any

Index 指定读取,这个仅能使用于 *Map 中有子 *Map。功能用于快速索引定位。

参:
  key ...any        快速指定父子关系中的值,如 .Index("A", "B", "C")
返:
  any               读取值

func (*Map) IndexHas

func (T *Map) IndexHas(key ...any) (any, bool)

IndexHas 指定读取和判断,这个只有使用于 *Map 中有子 *Map。功能用于快速索引定位。

key ...any        快速指定父子关系中的值,如 .Index("A", "B", "C")
any               读取值
bool                      判断,如果为true,表示存在。否则为flase
例:
  m1 := birdswo.NewMap()
  m2 := birdswo.NewMap()
  m2.Set("b", "value")
  m1.Set("a", m2)
  v, ok := m1.IndexHas("a", "b")
  fmt.Println(v, ok)
  //value true

func (*Map) Keys added in v2.3.0

func (T *Map) Keys() []any

Keys 以列表形式返回map中的所有键名

[]any	所有键名

func (*Map) Len

func (T *Map) Len() int

Len 长度

int   长度

func (*Map) MarshalJSON

func (T *Map) MarshalJSON() ([]byte, error)

MarshalJSON 转JSON

[]byte    字节格式的json
error     错误,格式无法压缩,导致 json.Marshal 发生错误。

func (*Map) New

func (T *Map) New(key any) *Map

New 增加一个key对应的Map对象,并返回该对象。 .New 不管是否存在该 key 键值,都会写入一个新的Map覆盖 key 键值。 要是你需要一个这样的功能,存在key键值,返回该key键值。如果不存在该key键值,返回一个新的key对应的Map。请使用 .GetNewMap 方法

key any     键名
*Map                Map对象

func (*Map) Range

func (T *Map) Range(f func(key, value any) bool)

遍历

f func(key, value any	遍历函数

func (*Map) ReadAll

func (T *Map) ReadAll() any

ReadAll 读取所有

any   复制一份Map

func (*Map) ReadFrom

func (T *Map) ReadFrom(mm any) error

ReadFrom 从mm中读取 map

mm any      从mm中读取
error               错误,mm类型不是map,发生错误。

func (*Map) Reset

func (T *Map) Reset()

Reset 重置归零

func (*Map) Set

func (T *Map) Set(key, val any)

Set 设置,如果你设置的值是Map,将会被强制初始化该值。这样避免读取并调用时候出错。

key any   键名
val any   值

func (*Map) SetExpired

func (T *Map) SetExpired(key any, d time.Duration)

SetExpired 单个键值的有效期

key any		键名
d time.Duration		时间

func (*Map) SetExpiredCall

func (T *Map) SetExpiredCall(key any, d time.Duration, f func(any))

SetExpiredCall 单个键值的有效期,过期后并调用函数

key any		键名
d time.Duration		时间
f func(interface)	函数,过期调用,键删除调用

func (*Map) String

func (T *Map) String() string

String 字符串

string    字符串

func (*Map) UnmarshalJSON

func (T *Map) UnmarshalJSON(data []byte) error

UnmarshalJSON JSON转Map,格式需要是 map[string]any

data []byte    字节格式的json
error          错误,格式无法解压,导致 json.Unmarshal 发生错误。

func (*Map) Values added in v2.3.0

func (T *Map) Values() []any

Values 以列表形式返回map中的所有值

[]any	所有值

func (*Map) WriteTo

func (T *Map) WriteTo(mm any) (err error)

WriteTo 写入到 mm

mm any     写入到mm
error              错误,mm类型不是map,发生错误。

Jump to

Keyboard shortcuts

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