gmap

package
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2022 License: MIT Imports: 9 Imported by: 261

Documentation

Overview

Package gmap provides most commonly used map container which also support concurrent-safe/unsafe switch feature.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AnyAnyMap

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

func NewAnyAnyMap

func NewAnyAnyMap(safe ...bool) *AnyAnyMap

NewAnyAnyMap creates and returns an empty hash map. The parameter `safe` is used to specify whether using map in concurrent-safety, which is false in default.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewAnyAnyMap()

	m.Set("key1", "val1")
	fmt.Println(m)

}
Output:

{"key1":"val1"}

func NewAnyAnyMapFrom

func NewAnyAnyMapFrom(data map[interface{}]interface{}, safe ...bool) *AnyAnyMap

NewAnyAnyMapFrom creates and returns a hash map from given map `data`. Note that, the param `data` map will be set as the underlying data map(no deep copy), there might be some concurrent-safe issues when changing the map outside.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewAnyAnyMap()

	m.Set("key1", "val1")
	fmt.Println(m)

	n := gmap.NewAnyAnyMapFrom(m.MapCopy(), true)
	fmt.Println(n)

}
Output:

{"key1":"val1"}
{"key1":"val1"}

func (*AnyAnyMap) Clear

func (m *AnyAnyMap) Clear()

Clear deletes all data of the map, it will remake a new underlying data map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.Map
	m.Sets(g.MapAnyAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})

	m.Clear()

	fmt.Println(m.Map())

}
Output:

map[]

func (*AnyAnyMap) Clone

func (m *AnyAnyMap) Clone(safe ...bool) *AnyAnyMap

Clone returns a new hash map with copy of current map data.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.New()

	m.Set("key1", "val1")
	fmt.Println(m)

	n := m.Clone()
	fmt.Println(n)

}
Output:

{"key1":"val1"}
{"key1":"val1"}

func (*AnyAnyMap) Contains

func (m *AnyAnyMap) Contains(key interface{}) bool

Contains checks whether a key exists. It returns true if the `key` exists, or else false.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.Map
	m.Sets(g.MapAnyAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})

	fmt.Println(m.Contains("k1"))
	fmt.Println(m.Contains("k5"))

}
Output:

true
false

func (*AnyAnyMap) FilterEmpty

func (m *AnyAnyMap) FilterEmpty()

FilterEmpty deletes all key-value pair of which the value is empty. Values like: 0, nil, false, "", len(slice/map/chan) == 0 are considered empty.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	m := gmap.NewFrom(g.MapAnyAny{
		"k1": "",
		"k2": nil,
		"k3": 0,
		"k4": 1,
	})
	m.FilterEmpty()
	fmt.Println(m.Map())

}
Output:

map[k4:1]

func (*AnyAnyMap) FilterNil

func (m *AnyAnyMap) FilterNil()

FilterNil deletes all key-value pair of which the value is nil.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	m := gmap.NewFrom(g.MapAnyAny{
		"k1": "",
		"k2": nil,
		"k3": 0,
		"k4": 1,
	})
	m.FilterNil()
	fmt.Printf("%#v", m.Map())

}
Output:

map[interface {}]interface {}{"k1":"", "k3":0, "k4":1}

func (*AnyAnyMap) Flip

func (m *AnyAnyMap) Flip()

Flip exchanges key-value of the map to value-key.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.Map
	m.Sets(g.MapAnyAny{
		"k1": "v1",
	})
	m.Flip()
	fmt.Println(m.Map())

}
Output:

map[v1:k1]

func (*AnyAnyMap) Get

func (m *AnyAnyMap) Get(key interface{}) (value interface{})

Get returns the value by given `key`.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.New()

	m.Set("key1", "val1")

	fmt.Println("key1 value:", m.Get("key1"))
	fmt.Println("key2 value:", m.Get("key2"))

}
Output:

key1 value: val1
key2 value: <nil>

func (*AnyAnyMap) GetOrSet

func (m *AnyAnyMap) GetOrSet(key interface{}, value interface{}) interface{}

GetOrSet returns the value by key, or sets value with given `value` if it does not exist and then returns this value.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.New()
	m.Set("key1", "val1")

	fmt.Println(m.GetOrSet("key1", "NotExistValue"))
	fmt.Println(m.GetOrSet("key2", "val2"))

}
Output:

val1
val2

func (*AnyAnyMap) GetOrSetFunc

func (m *AnyAnyMap) GetOrSetFunc(key interface{}, f func() interface{}) interface{}

GetOrSetFunc returns the value by key, or sets value with returned value of callback function `f` if it does not exist and then returns this value.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.New()
	m.Set("key1", "val1")

	fmt.Println(m.GetOrSetFunc("key1", func() interface{} {
		return "NotExistValue"
	}))
	fmt.Println(m.GetOrSetFunc("key2", func() interface{} {
		return "NotExistValue"
	}))

}
Output:

val1
NotExistValue

func (*AnyAnyMap) GetOrSetFuncLock

func (m *AnyAnyMap) GetOrSetFuncLock(key interface{}, f func() interface{}) interface{}

GetOrSetFuncLock returns the value by key, or sets value with returned value of callback function `f` if it does not exist and then returns this value.

GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` with mutex.Lock of the hash map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.New()
	m.Set("key1", "val1")

	fmt.Println(m.GetOrSetFuncLock("key1", func() interface{} {
		return "NotExistValue"
	}))
	fmt.Println(m.GetOrSetFuncLock("key2", func() interface{} {
		return "NotExistValue"
	}))

}
Output:

val1
NotExistValue

func (*AnyAnyMap) GetVar

func (m *AnyAnyMap) GetVar(key interface{}) *gvar.Var

GetVar returns a Var with the value by given `key`. The returned Var is un-concurrent safe.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.New()
	m.Set("key1", "val1")

	fmt.Println(m.GetVar("key1"))
	fmt.Println(m.GetVar("key2").IsNil())

}
Output:

val1
true

func (*AnyAnyMap) GetVarOrSet

func (m *AnyAnyMap) GetVarOrSet(key interface{}, value interface{}) *gvar.Var

GetVarOrSet returns a Var with result from GetOrSet. The returned Var is un-concurrent safe.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.New()
	m.Set("key1", "val1")

	fmt.Println(m.GetVarOrSet("key1", "NotExistValue"))
	fmt.Println(m.GetVarOrSet("key2", "val2"))

}
Output:

val1
val2

func (*AnyAnyMap) GetVarOrSetFunc

func (m *AnyAnyMap) GetVarOrSetFunc(key interface{}, f func() interface{}) *gvar.Var

GetVarOrSetFunc returns a Var with result from GetOrSetFunc. The returned Var is un-concurrent safe.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.New()
	m.Set("key1", "val1")

	fmt.Println(m.GetVarOrSetFunc("key1", func() interface{} {
		return "NotExistValue"
	}))
	fmt.Println(m.GetVarOrSetFunc("key2", func() interface{} {
		return "NotExistValue"
	}))

}
Output:

val1
NotExistValue

func (*AnyAnyMap) GetVarOrSetFuncLock

func (m *AnyAnyMap) GetVarOrSetFuncLock(key interface{}, f func() interface{}) *gvar.Var

GetVarOrSetFuncLock returns a Var with result from GetOrSetFuncLock. The returned Var is un-concurrent safe.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.New()
	m.Set("key1", "val1")

	fmt.Println(m.GetVarOrSetFuncLock("key1", func() interface{} {
		return "NotExistValue"
	}))
	fmt.Println(m.GetVarOrSetFuncLock("key2", func() interface{} {
		return "NotExistValue"
	}))

}
Output:

val1
NotExistValue

func (*AnyAnyMap) IsEmpty

func (m *AnyAnyMap) IsEmpty() bool

IsEmpty checks whether the map is empty. It returns true if map is empty, or else false.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.Map
	fmt.Println(m.IsEmpty())

	m.Set("k1", "v1")
	fmt.Println(m.IsEmpty())

}
Output:

true
false

func (*AnyAnyMap) Iterator

func (m *AnyAnyMap) Iterator(f func(k interface{}, v interface{}) bool)

Iterator iterates the hash map readonly with custom callback function `f`. If `f` returns true, then it continues iterating; or false to stop.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.New()
	for i := 0; i < 10; i++ {
		m.Set(i, i*2)
	}

	var totalKey, totalValue int
	m.Iterator(func(k interface{}, v interface{}) bool {
		totalKey += k.(int)
		totalValue += v.(int)

		return totalKey < 10
	})

	fmt.Println("totalKey:", totalKey)
	fmt.Println("totalValue:", totalValue)

	// May Output:
	// totalKey: 11
	// totalValue: 22
}
Output:

func (*AnyAnyMap) Keys

func (m *AnyAnyMap) Keys() []interface{}

Keys returns all keys of the map as a slice.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.Map
	m.Sets(g.MapAnyAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})
	fmt.Println(m.Keys())

	// May Output:
	// [k1 k2 k3 k4]
}
Output:

func (*AnyAnyMap) LockFunc

func (m *AnyAnyMap) LockFunc(f func(m map[interface{}]interface{}))

LockFunc locks writing with given callback function `f` within RWMutex.Lock.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.Map
	m.Sets(g.MapAnyAny{
		"k1": 1,
		"k2": 2,
		"k3": 3,
		"k4": 4,
	})

	m.LockFunc(func(m map[interface{}]interface{}) {
		totalValue := 0
		for _, v := range m {
			totalValue += v.(int)
		}
		fmt.Println("totalValue:", totalValue)
	})

}
Output:

totalValue: 10

func (*AnyAnyMap) Map

func (m *AnyAnyMap) Map() map[interface{}]interface{}

Map returns the underlying data map. Note that, if it's in concurrent-safe usage, it returns a copy of underlying data, or else a pointer to the underlying data.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	// non concurrent-safety, a pointer to the underlying data
	m1 := gmap.New()
	m1.Set("key1", "val1")
	fmt.Println("m1:", m1)

	n1 := m1.Map()
	fmt.Println("before n1:", n1)
	m1.Set("key1", "val2")
	fmt.Println("after n1:", n1)

	// concurrent-safety, copy of underlying data
	m2 := gmap.New(true)
	m2.Set("key1", "val1")
	fmt.Println("m2:", m2)

	n2 := m2.Map()
	fmt.Println("before n2:", n2)
	m2.Set("key1", "val2")
	fmt.Println("after n2:", n2)

}
Output:

m1: {"key1":"val1"}
before n1: map[key1:val1]
after n1: map[key1:val2]
m2: {"key1":"val1"}
before n2: map[key1:val1]
after n2: map[key1:val1]

func (*AnyAnyMap) MapCopy

func (m *AnyAnyMap) MapCopy() map[interface{}]interface{}

MapCopy returns a copy of the underlying data of the hash map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.New()

	m.Set("key1", "val1")
	m.Set("key2", "val2")
	fmt.Println(m)

	n := m.MapCopy()
	fmt.Println(n)

}
Output:

{"key1":"val1","key2":"val2"}
map[key1:val1 key2:val2]

func (*AnyAnyMap) MapStrAny

func (m *AnyAnyMap) MapStrAny() map[string]interface{}

MapStrAny returns a copy of the underlying data of the map as map[string]interface{}.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.New()
	m.Set(1001, "val1")
	m.Set(1002, "val2")

	n := m.MapStrAny()
	fmt.Printf("%#v", n)

}
Output:

map[string]interface {}{"1001":"val1", "1002":"val2"}

func (AnyAnyMap) MarshalJSON

func (m AnyAnyMap) MarshalJSON() ([]byte, error)

MarshalJSON implements the interface MarshalJSON for json.Marshal.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/internal/json"
	"github.com/gogf/gf/v2/util/gconv"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.Map
	m.Sets(g.MapAnyAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})

	bytes, err := json.Marshal(&m)
	if err == nil {
		fmt.Println(gconv.String(bytes))
	}

}
Output:

{"k1":"v1","k2":"v2","k3":"v3","k4":"v4"}

func (*AnyAnyMap) Merge

func (m *AnyAnyMap) Merge(other *AnyAnyMap)

Merge merges two hash maps. The `other` map will be merged into the map `m`.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m1, m2 gmap.Map
	m1.Set("key1", "val1")
	m2.Set("key2", "val2")
	m1.Merge(&m2)
	fmt.Println(m1.Map())

	// May Output:
	// map[key1:val1 key2:val2]
}
Output:

func (*AnyAnyMap) Pop

func (m *AnyAnyMap) Pop() (key, value interface{})

Pop retrieves and deletes an item from the map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.Map
	m.Sets(g.MapAnyAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})

	fmt.Println(m.Pop())

	// May Output:
	// k1 v1
}
Output:

func (*AnyAnyMap) Pops

func (m *AnyAnyMap) Pops(size int) map[interface{}]interface{}

Pops retrieves and deletes `size` items from the map. It returns all items if size == -1.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.Map
	m.Sets(g.MapAnyAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})
	fmt.Println(m.Pops(-1))
	fmt.Println("size:", m.Size())

	m.Sets(g.MapAnyAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})
	fmt.Println(m.Pops(2))
	fmt.Println("size:", m.Size())

	// May Output:
	// map[k1:v1 k2:v2 k3:v3 k4:v4]
	// size: 0
	// map[k1:v1 k2:v2]
	// size: 2
}
Output:

func (*AnyAnyMap) RLockFunc

func (m *AnyAnyMap) RLockFunc(f func(m map[interface{}]interface{}))

RLockFunc locks reading with given callback function `f` within RWMutex.RLock.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.Map
	m.Sets(g.MapAnyAny{
		"k1": 1,
		"k2": 2,
		"k3": 3,
		"k4": 4,
	})

	m.RLockFunc(func(m map[interface{}]interface{}) {
		totalValue := 0
		for _, v := range m {
			totalValue += v.(int)
		}
		fmt.Println("totalValue:", totalValue)
	})

}
Output:

totalValue: 10

func (*AnyAnyMap) Remove

func (m *AnyAnyMap) Remove(key interface{}) (value interface{})

Remove deletes value from map by given `key`, and return this deleted value.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.Map
	m.Set("k1", "v1")

	fmt.Println(m.Remove("k1"))
	fmt.Println(m.Remove("k2"))
	fmt.Println(m.Size())

}
Output:

v1
<nil>
0

func (*AnyAnyMap) Removes

func (m *AnyAnyMap) Removes(keys []interface{})

Removes batch deletes values of the map by keys.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.Map
	m.Sets(g.MapAnyAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})

	removeList := make([]interface{}, 2)
	removeList = append(removeList, "k1")
	removeList = append(removeList, "k2")

	m.Removes(removeList)

	fmt.Println(m.Map())

}
Output:

map[k3:v3 k4:v4]

func (*AnyAnyMap) Replace

func (m *AnyAnyMap) Replace(data map[interface{}]interface{})

Replace the data of the map with given `data`.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.Map
	m.Sets(g.MapAnyAny{
		"k1": "v1",
	})

	var n gmap.Map
	n.Sets(g.MapAnyAny{
		"k2": "v2",
	})

	fmt.Println(m.Map())

	m.Replace(n.Map())
	fmt.Println(m.Map())

	n.Set("k2", "v1")
	fmt.Println(m.Map())

}
Output:

map[k1:v1]
map[k2:v2]
map[k2:v1]

func (*AnyAnyMap) Search

func (m *AnyAnyMap) Search(key interface{}) (value interface{}, found bool)

Search searches the map with given `key`. Second return parameter `found` is true if key was found, otherwise false.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.New()

	m.Set("key1", "val1")

	value, found := m.Search("key1")
	if found {
		fmt.Println("find key1 value:", value)
	}

	value, found = m.Search("key2")
	if !found {
		fmt.Println("key2 not find")
	}

}
Output:

find key1 value: val1
key2 not find

func (*AnyAnyMap) Set

func (m *AnyAnyMap) Set(key interface{}, value interface{})

Set sets key-value to the hash map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.New()

	m.Set("key1", "val1")
	fmt.Println(m)

}
Output:

{"key1":"val1"}

func (*AnyAnyMap) SetIfNotExist

func (m *AnyAnyMap) SetIfNotExist(key interface{}, value interface{}) bool

SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true. It returns false if `key` exists, and `value` would be ignored.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.Map
	fmt.Println(m.SetIfNotExist("k1", "v1"))
	fmt.Println(m.SetIfNotExist("k1", "v2"))
	fmt.Println(m.Map())

}
Output:

true
false
map[k1:v1]

func (*AnyAnyMap) SetIfNotExistFunc

func (m *AnyAnyMap) SetIfNotExistFunc(key interface{}, f func() interface{}) bool

SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and `value` would be ignored.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.Map
	fmt.Println(m.SetIfNotExistFunc("k1", func() interface{} {
		return "v1"
	}))
	fmt.Println(m.SetIfNotExistFunc("k1", func() interface{} {
		return "v2"
	}))
	fmt.Println(m.Map())

}
Output:

true
false
map[k1:v1]

func (*AnyAnyMap) SetIfNotExistFuncLock

func (m *AnyAnyMap) SetIfNotExistFuncLock(key interface{}, f func() interface{}) bool

SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and `value` would be ignored.

SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that it executes function `f` with mutex.Lock of the hash map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.Map
	fmt.Println(m.SetIfNotExistFuncLock("k1", func() interface{} {
		return "v1"
	}))
	fmt.Println(m.SetIfNotExistFuncLock("k1", func() interface{} {
		return "v2"
	}))
	fmt.Println(m.Map())

}
Output:

true
false
map[k1:v1]

func (*AnyAnyMap) Sets

func (m *AnyAnyMap) Sets(data map[interface{}]interface{})

Sets batch sets key-values to the hash map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.New()

	addMap := make(map[interface{}]interface{})
	addMap["key1"] = "val1"
	addMap["key2"] = "val2"
	addMap["key3"] = "val3"

	m.Sets(addMap)
	fmt.Println(m)

}
Output:

{"key1":"val1","key2":"val2","key3":"val3"}

func (*AnyAnyMap) Size

func (m *AnyAnyMap) Size() int

Size returns the size of the map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.Map
	m.Sets(g.MapAnyAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})

	fmt.Println(m.Size())

}
Output:

4

func (*AnyAnyMap) String

func (m *AnyAnyMap) String() string

String returns the map as a string.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.Map
	m.Sets(g.MapAnyAny{
		"k1": "v1",
	})

	fmt.Println(m.String())

}
Output:

{"k1":"v1"}

func (*AnyAnyMap) UnmarshalJSON

func (m *AnyAnyMap) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/internal/json"
	"github.com/gogf/gf/v2/util/gconv"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.Map
	m.Sets(g.MapAnyAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})

	var n gmap.Map

	err := json.Unmarshal(gconv.Bytes(m.String()), &n)
	if err == nil {
		fmt.Println(n.Map())
	}

}
Output:

map[k1:v1 k2:v2 k3:v3 k4:v4]

func (*AnyAnyMap) UnmarshalValue

func (m *AnyAnyMap) UnmarshalValue(value interface{}) (err error)

UnmarshalValue is an interface implement which sets any type of value for map.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/util/gconv"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	type User struct {
		Uid   int
		Name  string
		Pass1 string `gconv:"password1"`
		Pass2 string `gconv:"password2"`
	}

	var (
		m    gmap.AnyAnyMap
		user = User{
			Uid:   1,
			Name:  "john",
			Pass1: "123",
			Pass2: "456",
		}
	)
	if err := gconv.Scan(user, &m); err == nil {
		fmt.Printf("%#v", m.Map())
	}

}
Output:

map[interface {}]interface {}{"Name":"john", "Uid":1, "password1":"123", "password2":"456"}

func (*AnyAnyMap) Values

func (m *AnyAnyMap) Values() []interface{}

Values returns all values of the map as a slice.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.Map
	m.Sets(g.MapAnyAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})
	fmt.Println(m.Values())

	// May Output:
	// [v1 v2 v3 v4]
}
Output:

type HashMap

type HashMap = AnyAnyMap // HashMap is alias of AnyAnyMap.

type IntAnyMap

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

func NewIntAnyMap

func NewIntAnyMap(safe ...bool) *IntAnyMap

NewIntAnyMap returns an empty IntAnyMap object. The parameter `safe` is used to specify whether using map in concurrent-safety, which is false in default.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewIntAnyMap()

	m.Set(1, "val1")
	fmt.Println(m)

}
Output:

{"1":"val1"}

func NewIntAnyMapFrom

func NewIntAnyMapFrom(data map[int]interface{}, safe ...bool) *IntAnyMap

NewIntAnyMapFrom creates and returns a hash map from given map `data`. Note that, the param `data` map will be set as the underlying data map(no deep copy), there might be some concurrent-safe issues when changing the map outside.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewIntAnyMap()

	m.Set(1, "val1")
	fmt.Println(m)

	n := gmap.NewIntAnyMapFrom(m.MapCopy(), true)
	fmt.Println(n)

}
Output:

{"1":"val1"}
{"1":"val1"}

func (*IntAnyMap) Clear

func (m *IntAnyMap) Clear()

Clear deletes all data of the map, it will remake a new underlying data map.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.IntAnyMap
	m.Sets(g.MapIntAny{
		1: "v1",
		2: "v2",
		3: "v3",
		4: "v4",
	})

	m.Clear()

	fmt.Println(m.Map())

}
Output:

map[]

func (*IntAnyMap) Clone

func (m *IntAnyMap) Clone() *IntAnyMap

Clone returns a new hash map with copy of current map data.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewIntAnyMap()

	m.Set(1, "val1")
	fmt.Println(m)

	n := m.Clone()
	fmt.Println(n)

}
Output:

{"1":"val1"}
{"1":"val1"}

func (*IntAnyMap) Contains

func (m *IntAnyMap) Contains(key int) bool

Contains checks whether a key exists. It returns true if the `key` exists, or else false.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.IntAnyMap
	m.Sets(g.MapIntAny{
		1: "v1",
		2: "v2",
		3: "v3",
		4: "v4",
	})

	fmt.Println(m.Contains(1))
	fmt.Println(m.Contains(5))

}
Output:

true
false

func (*IntAnyMap) FilterEmpty

func (m *IntAnyMap) FilterEmpty()

FilterEmpty deletes all key-value pair of which the value is empty. Values like: 0, nil, false, "", len(slice/map/chan) == 0 are considered empty.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	m := gmap.NewIntAnyMapFrom(g.MapIntAny{
		1: "",
		2: nil,
		3: 0,
		4: 1,
	})
	m.FilterEmpty()
	fmt.Println(m.Map())

}
Output:

map[4:1]

func (*IntAnyMap) FilterNil

func (m *IntAnyMap) FilterNil()

FilterNil deletes all key-value pair of which the value is nil.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	m := gmap.NewIntAnyMapFrom(g.MapIntAny{
		1: "",
		2: nil,
		3: 0,
		4: 1,
	})
	m.FilterNil()
	fmt.Printf("%#v", m.Map())

}
Output:

map[int]interface {}{1:"", 3:0, 4:1}

func (*IntAnyMap) Flip

func (m *IntAnyMap) Flip()

Flip exchanges key-value of the map to value-key.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.IntAnyMap
	m.Sets(g.MapIntAny{
		1: 10,
	})
	m.Flip()
	fmt.Println(m.Map())

}
Output:

map[10:1]

func (*IntAnyMap) Get

func (m *IntAnyMap) Get(key int) (value interface{})

Get returns the value by given `key`.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewIntAnyMap()

	m.Set(1, "val1")

	fmt.Println("key1 value:", m.Get(1))
	fmt.Println("key2 value:", m.Get(2))

}
Output:

key1 value: val1
key2 value: <nil>

func (*IntAnyMap) GetOrSet

func (m *IntAnyMap) GetOrSet(key int, value interface{}) interface{}

GetOrSet returns the value by key, or sets value with given `value` if it does not exist and then returns this value.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewIntAnyMap()
	m.Set(1, "val1")

	fmt.Println(m.GetOrSet(1, "NotExistValue"))
	fmt.Println(m.GetOrSet(2, "val2"))

}
Output:

val1
val2

func (*IntAnyMap) GetOrSetFunc

func (m *IntAnyMap) GetOrSetFunc(key int, f func() interface{}) interface{}

GetOrSetFunc returns the value by key, or sets value with returned value of callback function `f` if it does not exist and returns this value.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewIntAnyMap()
	m.Set(1, "val1")

	fmt.Println(m.GetOrSetFunc(1, func() interface{} {
		return "NotExistValue"
	}))
	fmt.Println(m.GetOrSetFunc(2, func() interface{} {
		return "NotExistValue"
	}))

}
Output:

val1
NotExistValue

func (*IntAnyMap) GetOrSetFuncLock

func (m *IntAnyMap) GetOrSetFuncLock(key int, f func() interface{}) interface{}

GetOrSetFuncLock returns the value by key, or sets value with returned value of callback function `f` if it does not exist and returns this value.

GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` with mutex.Lock of the hash map.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewIntAnyMap()
	m.Set(1, "val1")

	fmt.Println(m.GetOrSetFuncLock(1, func() interface{} {
		return "NotExistValue"
	}))
	fmt.Println(m.GetOrSetFuncLock(2, func() interface{} {
		return "NotExistValue"
	}))

}
Output:

val1
NotExistValue

func (*IntAnyMap) GetVar

func (m *IntAnyMap) GetVar(key int) *gvar.Var

GetVar returns a Var with the value by given `key`. The returned Var is un-concurrent safe.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewIntAnyMap()
	m.Set(1, "val1")

	fmt.Println(m.GetVar(1))
	fmt.Println(m.GetVar(2).IsNil())

}
Output:

val1
true

func (*IntAnyMap) GetVarOrSet

func (m *IntAnyMap) GetVarOrSet(key int, value interface{}) *gvar.Var

GetVarOrSet returns a Var with result from GetVarOrSet. The returned Var is un-concurrent safe.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewIntAnyMap()
	m.Set(1, "val1")

	fmt.Println(m.GetVarOrSet(1, "NotExistValue"))
	fmt.Println(m.GetVarOrSet(2, "val2"))

}
Output:

val1
val2

func (*IntAnyMap) GetVarOrSetFunc

func (m *IntAnyMap) GetVarOrSetFunc(key int, f func() interface{}) *gvar.Var

GetVarOrSetFunc returns a Var with result from GetOrSetFunc. The returned Var is un-concurrent safe.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewIntAnyMap()
	m.Set(1, "val1")

	fmt.Println(m.GetVarOrSetFunc(1, func() interface{} {
		return "NotExistValue"
	}))
	fmt.Println(m.GetVarOrSetFunc(2, func() interface{} {
		return "NotExistValue"
	}))

}
Output:

val1
NotExistValue

func (*IntAnyMap) GetVarOrSetFuncLock

func (m *IntAnyMap) GetVarOrSetFuncLock(key int, f func() interface{}) *gvar.Var

GetVarOrSetFuncLock returns a Var with result from GetOrSetFuncLock. The returned Var is un-concurrent safe.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewIntAnyMap()
	m.Set(1, "val1")

	fmt.Println(m.GetVarOrSetFuncLock(1, func() interface{} {
		return "NotExistValue"
	}))
	fmt.Println(m.GetVarOrSetFuncLock(2, func() interface{} {
		return "NotExistValue"
	}))

}
Output:

val1
NotExistValue

func (*IntAnyMap) IsEmpty

func (m *IntAnyMap) IsEmpty() bool

IsEmpty checks whether the map is empty. It returns true if map is empty, or else false.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.IntAnyMap
	fmt.Println(m.IsEmpty())

	m.Set(1, "v1")
	fmt.Println(m.IsEmpty())

}
Output:

true
false

func (*IntAnyMap) Iterator

func (m *IntAnyMap) Iterator(f func(k int, v interface{}) bool)

Iterator iterates the hash map readonly with custom callback function `f`. If `f` returns true, then it continues iterating; or false to stop.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewIntAnyMap()
	for i := 0; i < 10; i++ {
		m.Set(i, i*2)
	}

	var totalKey, totalValue int
	m.Iterator(func(k int, v interface{}) bool {
		totalKey += k
		totalValue += v.(int)

		return totalKey < 10
	})

	fmt.Println("totalKey:", totalKey)
	fmt.Println("totalValue:", totalValue)

	// May Output:
	// totalKey: 11
	// totalValue: 22
}
Output:

func (*IntAnyMap) Keys

func (m *IntAnyMap) Keys() []int

Keys returns all keys of the map as a slice.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.IntAnyMap
	m.Sets(g.MapIntAny{
		1: "v1",
		2: "v2",
		3: "v3",
		4: "v4",
	})
	fmt.Println(m.Keys())

	// May Output:
	// [1 2 3 4]
}
Output:

func (*IntAnyMap) LockFunc

func (m *IntAnyMap) LockFunc(f func(m map[int]interface{}))

LockFunc locks writing with given callback function `f` within RWMutex.Lock.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.IntAnyMap
	m.Sets(g.MapIntAny{
		1: 1,
		2: 2,
		3: 3,
		4: 4,
	})

	m.LockFunc(func(m map[int]interface{}) {
		totalValue := 0
		for _, v := range m {
			totalValue += v.(int)
		}
		fmt.Println("totalValue:", totalValue)
	})

}
Output:

totalValue: 10

func (*IntAnyMap) Map

func (m *IntAnyMap) Map() map[int]interface{}

Map returns the underlying data map. Note that, if it's in concurrent-safe usage, it returns a copy of underlying data, or else a pointer to the underlying data.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	// non concurrent-safety, a pointer to the underlying data
	m1 := gmap.NewIntAnyMap()
	m1.Set(1, "val1")
	fmt.Println("m1:", m1)

	n1 := m1.Map()
	fmt.Println("before n1:", n1)
	m1.Set(1, "val2")
	fmt.Println("after n1:", n1)

	// concurrent-safety, copy of underlying data
	m2 := gmap.New(true)
	m2.Set(1, "val1")
	fmt.Println("m2:", m2)

	n2 := m2.Map()
	fmt.Println("before n2:", n2)
	m2.Set(1, "val2")
	fmt.Println("after n2:", n2)

}
Output:

m1: {"1":"val1"}
before n1: map[1:val1]
after n1: map[1:val2]
m2: {"1":"val1"}
before n2: map[1:val1]
after n2: map[1:val1]

func (*IntAnyMap) MapCopy

func (m *IntAnyMap) MapCopy() map[int]interface{}

MapCopy returns a copy of the underlying data of the hash map.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewIntAnyMap()

	m.Set(1, "val1")
	m.Set(2, "val2")
	fmt.Println(m)

	n := m.MapCopy()
	fmt.Println(n)

}
Output:

{"1":"val1","2":"val2"}
map[1:val1 2:val2]

func (*IntAnyMap) MapStrAny

func (m *IntAnyMap) MapStrAny() map[string]interface{}

MapStrAny returns a copy of the underlying data of the map as map[string]interface{}.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewIntAnyMap()
	m.Set(1001, "val1")
	m.Set(1002, "val2")

	n := m.MapStrAny()
	fmt.Printf("%#v", n)

}
Output:

map[string]interface {}{"1001":"val1", "1002":"val2"}

func (IntAnyMap) MarshalJSON

func (m IntAnyMap) MarshalJSON() ([]byte, error)

MarshalJSON implements the interface MarshalJSON for json.Marshal.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/internal/json"
	"github.com/gogf/gf/v2/util/gconv"
)

func main() {
	var m gmap.IntAnyMap
	m.Sets(g.MapIntAny{
		1: "v1",
		2: "v2",
		3: "v3",
		4: "v4",
	})

	bytes, err := json.Marshal(&m)
	if err == nil {
		fmt.Println(gconv.String(bytes))
	}

}
Output:

{"1":"v1","2":"v2","3":"v3","4":"v4"}

func (*IntAnyMap) Merge

func (m *IntAnyMap) Merge(other *IntAnyMap)

Merge merges two hash maps. The `other` map will be merged into the map `m`.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m1, m2 gmap.Map
	m1.Set(1, "val1")
	m2.Set(2, "val2")
	m1.Merge(&m2)
	fmt.Println(m1.Map())

	// May Output:
	// map[key1:val1 key2:val2]
}
Output:

func (*IntAnyMap) Pop

func (m *IntAnyMap) Pop() (key int, value interface{})

Pop retrieves and deletes an item from the map.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.IntAnyMap
	m.Sets(g.MapIntAny{
		1: "v1",
		2: "v2",
		3: "v3",
		4: "v4",
	})

	fmt.Println(m.Pop())

	// May Output:
	// 1 v1
}
Output:

func (*IntAnyMap) Pops

func (m *IntAnyMap) Pops(size int) map[int]interface{}

Pops retrieves and deletes `size` items from the map. It returns all items if size == -1.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.IntAnyMap
	m.Sets(g.MapIntAny{
		1: "v1",
		2: "v2",
		3: "v3",
		4: "v4",
	})
	fmt.Println(m.Pops(-1))
	fmt.Println("size:", m.Size())

	m.Sets(g.MapIntAny{
		1: "v1",
		2: "v2",
		3: "v3",
		4: "v4",
	})
	fmt.Println(m.Pops(2))
	fmt.Println("size:", m.Size())

	// May Output:
	// map[1:v1 2:v2 3:v3 4:v4]
	// size: 0
	// map[1:v1 2:v2]
	// size: 2
}
Output:

func (*IntAnyMap) RLockFunc

func (m *IntAnyMap) RLockFunc(f func(m map[int]interface{}))

RLockFunc locks reading with given callback function `f` within RWMutex.RLock.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.IntAnyMap
	m.Sets(g.MapIntAny{
		1: 1,
		2: 2,
		3: 3,
		4: 4,
	})

	m.RLockFunc(func(m map[int]interface{}) {
		totalValue := 0
		for _, v := range m {
			totalValue += v.(int)
		}
		fmt.Println("totalValue:", totalValue)
	})

}
Output:

totalValue: 10

func (*IntAnyMap) Remove

func (m *IntAnyMap) Remove(key int) (value interface{})

Remove deletes value from map by given `key`, and return this deleted value.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.IntAnyMap
	m.Set(1, "v1")

	fmt.Println(m.Remove(1))
	fmt.Println(m.Remove(2))
	fmt.Println(m.Size())

}
Output:

v1
<nil>
0

func (*IntAnyMap) Removes

func (m *IntAnyMap) Removes(keys []int)

Removes batch deletes values of the map by keys.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.IntAnyMap
	m.Sets(g.MapIntAny{
		1: "v1",
		2: "v2",
		3: "v3",
		4: "v4",
	})

	removeList := make([]int, 2)
	removeList = append(removeList, 1)
	removeList = append(removeList, 2)

	m.Removes(removeList)

	fmt.Println(m.Map())

}
Output:

map[3:v3 4:v4]

func (*IntAnyMap) Replace

func (m *IntAnyMap) Replace(data map[int]interface{})

Replace the data of the map with given `data`.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.IntAnyMap
	m.Sets(g.MapIntAny{
		1: "v1",
	})

	var n gmap.IntAnyMap
	n.Sets(g.MapIntAny{
		2: "v2",
	})

	fmt.Println(m.Map())

	m.Replace(n.Map())
	fmt.Println(m.Map())

	n.Set(2, "v1")
	fmt.Println(m.Map())

}
Output:

map[1:v1]
map[2:v2]
map[2:v1]

func (*IntAnyMap) Search

func (m *IntAnyMap) Search(key int) (value interface{}, found bool)

Search searches the map with given `key`. Second return parameter `found` is true if key was found, otherwise false.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewIntAnyMap()

	m.Set(1, "val1")

	value, found := m.Search(1)
	if found {
		fmt.Println("find key1 value:", value)
	}

	value, found = m.Search(2)
	if !found {
		fmt.Println("key2 not find")
	}

}
Output:

find key1 value: val1
key2 not find

func (*IntAnyMap) Set

func (m *IntAnyMap) Set(key int, val interface{})

Set sets key-value to the hash map.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewIntAnyMap()

	m.Set(1, "val1")
	fmt.Println(m)

}
Output:

{"1":"val1"}

func (*IntAnyMap) SetIfNotExist

func (m *IntAnyMap) SetIfNotExist(key int, value interface{}) bool

SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true. It returns false if `key` exists, and `value` would be ignored.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.IntAnyMap
	fmt.Println(m.SetIfNotExist(1, "v1"))
	fmt.Println(m.SetIfNotExist(1, "v2"))
	fmt.Println(m.Map())

}
Output:

true
false
map[1:v1]

func (*IntAnyMap) SetIfNotExistFunc

func (m *IntAnyMap) SetIfNotExistFunc(key int, f func() interface{}) bool

SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and `value` would be ignored.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.IntAnyMap
	fmt.Println(m.SetIfNotExistFunc(1, func() interface{} {
		return "v1"
	}))
	fmt.Println(m.SetIfNotExistFunc(1, func() interface{} {
		return "v2"
	}))
	fmt.Println(m.Map())

}
Output:

true
false
map[1:v1]

func (*IntAnyMap) SetIfNotExistFuncLock

func (m *IntAnyMap) SetIfNotExistFuncLock(key int, f func() interface{}) bool

SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and `value` would be ignored.

SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that it executes function `f` with mutex.Lock of the hash map.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.IntAnyMap
	fmt.Println(m.SetIfNotExistFuncLock(1, func() interface{} {
		return "v1"
	}))
	fmt.Println(m.SetIfNotExistFuncLock(1, func() interface{} {
		return "v2"
	}))
	fmt.Println(m.Map())

}
Output:

true
false
map[1:v1]

func (*IntAnyMap) Sets

func (m *IntAnyMap) Sets(data map[int]interface{})

Sets batch sets key-values to the hash map.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewIntAnyMap()

	addMap := make(map[int]interface{})
	addMap[1] = "val1"
	addMap[2] = "val2"
	addMap[3] = "val3"

	m.Sets(addMap)
	fmt.Println(m)

}
Output:

{"1":"val1","2":"val2","3":"val3"}

func (*IntAnyMap) Size

func (m *IntAnyMap) Size() int

Size returns the size of the map.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.IntAnyMap
	m.Sets(g.MapIntAny{
		1: "v1",
		2: "v2",
		3: "v3",
		4: "v4",
	})

	fmt.Println(m.Size())

}
Output:

4

func (*IntAnyMap) String

func (m *IntAnyMap) String() string

String returns the map as a string.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.IntAnyMap
	m.Sets(g.MapIntAny{
		1: "v1",
	})

	fmt.Println(m.String())

}
Output:

{"1":"v1"}

func (*IntAnyMap) UnmarshalJSON

func (m *IntAnyMap) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/internal/json"
	"github.com/gogf/gf/v2/util/gconv"
)

func main() {
	var m gmap.IntAnyMap
	m.Sets(g.MapIntAny{
		1: "v1",
		2: "v2",
		3: "v3",
		4: "v4",
	})

	var n gmap.Map

	err := json.Unmarshal(gconv.Bytes(m.String()), &n)
	if err == nil {
		fmt.Println(n.Map())
	}

}
Output:

map[1:v1 2:v2 3:v3 4:v4]

func (*IntAnyMap) UnmarshalValue

func (m *IntAnyMap) UnmarshalValue(value interface{}) (err error)

UnmarshalValue is an interface implement which sets any type of value for map.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/util/gconv"
)

func main() {
	var m gmap.IntAnyMap

	goWeb := map[int]interface{}{
		1: "goframe",
		2: "gin",
		3: "echo",
	}

	if err := gconv.Scan(goWeb, &m); err == nil {
		fmt.Printf("%#v", m.Map())
	}

}
Output:

map[int]interface {}{1:"goframe", 2:"gin", 3:"echo"}

func (*IntAnyMap) Values

func (m *IntAnyMap) Values() []interface{}

Values returns all values of the map as a slice.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.IntAnyMap
	m.Sets(g.MapIntAny{
		1: "v1",
		2: "v2",
		3: "v3",
		4: "v4",
	})
	fmt.Println(m.Values())

	// May Output:
	// [v1 v2 v3 v4]
}
Output:

type IntIntMap

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

func NewIntIntMap

func NewIntIntMap(safe ...bool) *IntIntMap

NewIntIntMap returns an empty IntIntMap object. The parameter `safe` is used to specify whether using map in concurrent-safety, which is false in default.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewIntIntMap()

	m.Set(1, 1)
	fmt.Println(m)

}
Output:

{"1":1}

func NewIntIntMapFrom

func NewIntIntMapFrom(data map[int]int, safe ...bool) *IntIntMap

NewIntIntMapFrom creates and returns a hash map from given map `data`. Note that, the param `data` map will be set as the underlying data map(no deep copy), there might be some concurrent-safe issues when changing the map outside.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewIntIntMap()

	m.Set(1, 1)
	fmt.Println(m)

	n := gmap.NewIntIntMapFrom(m.MapCopy(), true)
	fmt.Println(n)

}
Output:

{"1":1}
{"1":1}

func (*IntIntMap) Clear

func (m *IntIntMap) Clear()

Clear deletes all data of the map, it will remake a new underlying data map.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.IntIntMap
	m.Sets(g.MapIntInt{
		1: 1,
		2: 2,
		3: 3,
		4: 4,
	})

	m.Clear()

	fmt.Println(m.Map())

}
Output:

map[]

func (*IntIntMap) Clone

func (m *IntIntMap) Clone() *IntIntMap

Clone returns a new hash map with copy of current map data.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewIntIntMap()

	m.Set(1, 1)
	fmt.Println(m)

	n := m.Clone()
	fmt.Println(n)

}
Output:

{"1":1}
{"1":1}

func (*IntIntMap) Contains

func (m *IntIntMap) Contains(key int) bool

Contains checks whether a key exists. It returns true if the `key` exists, or else false.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.IntIntMap
	m.Sets(g.MapIntInt{
		1: 1,
		2: 2,
		3: 3,
		4: 4,
	})

	fmt.Println(m.Contains(1))
	fmt.Println(m.Contains(5))

}
Output:

true
false

func (*IntIntMap) FilterEmpty

func (m *IntIntMap) FilterEmpty()

FilterEmpty deletes all key-value pair of which the value is empty. Values like: 0, nil, false, "", len(slice/map/chan) == 0 are considered empty.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	m := gmap.NewIntIntMapFrom(g.MapIntInt{
		1: 0,
		2: 1,
	})
	m.FilterEmpty()
	fmt.Println(m.Map())

}
Output:

map[2:1]

func (*IntIntMap) Flip

func (m *IntIntMap) Flip()

Flip exchanges key-value of the map to value-key.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.IntIntMap
	m.Sets(g.MapIntInt{
		1: 10,
	})
	m.Flip()
	fmt.Println(m.Map())

}
Output:

map[10:1]

func (*IntIntMap) Get

func (m *IntIntMap) Get(key int) (value int)

Get returns the value by given `key`.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewIntIntMap()

	m.Set(1, 1)

	fmt.Println("key1 value:", m.Get(1))
	fmt.Println("key2 value:", m.Get(2))

}
Output:

key1 value: 1
key2 value: 0

func (*IntIntMap) GetOrSet

func (m *IntIntMap) GetOrSet(key int, value int) int

GetOrSet returns the value by key, or sets value with given `value` if it does not exist and then returns this value.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewIntIntMap()
	m.Set(1, 1)

	fmt.Println(m.GetOrSet(1, 0))
	fmt.Println(m.GetOrSet(2, 2))

}
Output:

1
2

func (*IntIntMap) GetOrSetFunc

func (m *IntIntMap) GetOrSetFunc(key int, f func() int) int

GetOrSetFunc returns the value by key, or sets value with returned value of callback function `f` if it does not exist and returns this value.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewIntIntMap()
	m.Set(1, 1)

	fmt.Println(m.GetOrSetFunc(1, func() int {
		return 0
	}))
	fmt.Println(m.GetOrSetFunc(2, func() int {
		return 0
	}))

}
Output:

1
0

func (*IntIntMap) GetOrSetFuncLock

func (m *IntIntMap) GetOrSetFuncLock(key int, f func() int) int

GetOrSetFuncLock returns the value by key, or sets value with returned value of callback function `f` if it does not exist and returns this value.

GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` with mutex.Lock of the hash map.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewIntIntMap()
	m.Set(1, 1)

	fmt.Println(m.GetOrSetFuncLock(1, func() int {
		return 0
	}))
	fmt.Println(m.GetOrSetFuncLock(2, func() int {
		return 0
	}))

}
Output:

1
0

func (*IntIntMap) IsEmpty

func (m *IntIntMap) IsEmpty() bool

IsEmpty checks whether the map is empty. It returns true if map is empty, or else false.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.IntIntMap
	fmt.Println(m.IsEmpty())

	m.Set(1, 1)
	fmt.Println(m.IsEmpty())

}
Output:

true
false

func (*IntIntMap) Iterator

func (m *IntIntMap) Iterator(f func(k int, v int) bool)

Iterator iterates the hash map readonly with custom callback function `f`. If `f` returns true, then it continues iterating; or false to stop.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewIntIntMap()
	for i := 0; i < 10; i++ {
		m.Set(i, i*2)
	}

	var totalKey, totalValue int
	m.Iterator(func(k int, v int) bool {
		totalKey += k
		totalValue += v

		return totalKey < 10
	})

	fmt.Println("totalKey:", totalKey)
	fmt.Println("totalValue:", totalValue)

	// May Output:
	// totalKey: 11
	// totalValue: 22
}
Output:

func (*IntIntMap) Keys

func (m *IntIntMap) Keys() []int

Keys returns all keys of the map as a slice.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.IntIntMap
	m.Sets(g.MapIntInt{
		1: 1,
		2: 2,
		3: 3,
		4: 4,
	})
	fmt.Println(m.Keys())

	// May Output:
	// [1 2 3 4]
}
Output:

func (*IntIntMap) LockFunc

func (m *IntIntMap) LockFunc(f func(m map[int]int))

LockFunc locks writing with given callback function `f` within RWMutex.Lock.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.IntIntMap
	m.Sets(g.MapIntInt{
		1: 1,
		2: 2,
		3: 3,
		4: 4,
	})

	m.LockFunc(func(m map[int]int) {
		totalValue := 0
		for _, v := range m {
			totalValue += v
		}
		fmt.Println("totalValue:", totalValue)
	})

}
Output:

totalValue: 10

func (*IntIntMap) Map

func (m *IntIntMap) Map() map[int]int

Map returns the underlying data map. Note that, if it's in concurrent-safe usage, it returns a copy of underlying data, or else a pointer to the underlying data.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	// non concurrent-safety, a pointer to the underlying data
	m1 := gmap.NewIntIntMap()
	m1.Set(1, 1)
	fmt.Println("m1:", m1)

	n1 := m1.Map()
	fmt.Println("before n1:", n1)
	m1.Set(1, 2)
	fmt.Println("after n1:", n1)

	// concurrent-safety, copy of underlying data
	m2 := gmap.New(true)
	m2.Set(1, "1")
	fmt.Println("m2:", m2)

	n2 := m2.Map()
	fmt.Println("before n2:", n2)
	m2.Set(1, "2")
	fmt.Println("after n2:", n2)

}
Output:

m1: {"1":1}
before n1: map[1:1]
after n1: map[1:2]
m2: {"1":"1"}
before n2: map[1:1]
after n2: map[1:1]

func (*IntIntMap) MapCopy

func (m *IntIntMap) MapCopy() map[int]int

MapCopy returns a copy of the underlying data of the hash map.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewIntIntMap()

	m.Set(1, 1)
	m.Set(2, 2)
	fmt.Println(m)

	n := m.MapCopy()
	fmt.Println(n)

}
Output:

{"1":1,"2":2}
map[1:1 2:2]

func (*IntIntMap) MapStrAny

func (m *IntIntMap) MapStrAny() map[string]interface{}

MapStrAny returns a copy of the underlying data of the map as map[string]interface{}.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewIntIntMap()
	m.Set(1001, 1)
	m.Set(1002, 2)

	n := m.MapStrAny()
	fmt.Printf("%#v", n)

}
Output:

map[string]interface {}{"1001":1, "1002":2}

func (IntIntMap) MarshalJSON

func (m IntIntMap) MarshalJSON() ([]byte, error)

MarshalJSON implements the interface MarshalJSON for json.Marshal.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/internal/json"
	"github.com/gogf/gf/v2/util/gconv"
)

func main() {
	var m gmap.IntIntMap
	m.Sets(g.MapIntInt{
		1: 1,
		2: 2,
		3: 3,
		4: 4,
	})

	bytes, err := json.Marshal(&m)
	if err == nil {
		fmt.Println(gconv.String(bytes))
	}

}
Output:

{"1":1,"2":2,"3":3,"4":4}

func (*IntIntMap) Merge

func (m *IntIntMap) Merge(other *IntIntMap)

Merge merges two hash maps. The `other` map will be merged into the map `m`.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m1, m2 gmap.Map
	m1.Set(1, "1")
	m2.Set(2, "2")
	m1.Merge(&m2)
	fmt.Println(m1.Map())

	// May Output:
	// map[key1:1 key2:2]
}
Output:

func (*IntIntMap) Pop

func (m *IntIntMap) Pop() (key, value int)

Pop retrieves and deletes an item from the map.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.IntIntMap
	m.Sets(g.MapIntInt{
		1: 1,
		2: 2,
		3: 3,
		4: 4,
	})

	fmt.Println(m.Pop())

	// May Output:
	// 1 1
}
Output:

func (*IntIntMap) Pops

func (m *IntIntMap) Pops(size int) map[int]int

Pops retrieves and deletes `size` items from the map. It returns all items if size == -1.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.IntIntMap
	m.Sets(g.MapIntInt{
		1: 1,
		2: 2,
		3: 3,
		4: 4,
	})
	fmt.Println(m.Pops(-1))
	fmt.Println("size:", m.Size())

	m.Sets(g.MapIntInt{
		1: 1,
		2: 2,
		3: 3,
		4: 4,
	})
	fmt.Println(m.Pops(2))
	fmt.Println("size:", m.Size())

	// May Output:
	// map[1:1 2:2 3:3 4:4]
	// size: 0
	// map[1:1 2:2]
	// size: 2
}
Output:

func (*IntIntMap) RLockFunc

func (m *IntIntMap) RLockFunc(f func(m map[int]int))

RLockFunc locks reading with given callback function `f` within RWMutex.RLock.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.IntIntMap
	m.Sets(g.MapIntInt{
		1: 1,
		2: 2,
		3: 3,
		4: 4,
	})

	m.RLockFunc(func(m map[int]int) {
		totalValue := 0
		for _, v := range m {
			totalValue += v
		}
		fmt.Println("totalValue:", totalValue)
	})

}
Output:

totalValue: 10

func (*IntIntMap) Remove

func (m *IntIntMap) Remove(key int) (value int)

Remove deletes value from map by given `key`, and return this deleted value.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.IntIntMap
	m.Set(1, 1)

	fmt.Println(m.Remove(1))
	fmt.Println(m.Remove(2))
	fmt.Println(m.Size())

}
Output:

1
0
0

func (*IntIntMap) Removes

func (m *IntIntMap) Removes(keys []int)

Removes batch deletes values of the map by keys.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.IntIntMap
	m.Sets(g.MapIntInt{
		1: 1,
		2: 2,
		3: 3,
		4: 4,
	})

	removeList := make([]int, 2)
	removeList = append(removeList, 1)
	removeList = append(removeList, 2)

	m.Removes(removeList)

	fmt.Println(m.Map())

}
Output:

map[3:3 4:4]

func (*IntIntMap) Replace

func (m *IntIntMap) Replace(data map[int]int)

Replace the data of the map with given `data`.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.IntIntMap
	m.Sets(g.MapIntInt{
		1: 1,
	})

	var n gmap.IntIntMap
	n.Sets(g.MapIntInt{
		2: 2,
	})

	fmt.Println(m.Map())

	m.Replace(n.Map())
	fmt.Println(m.Map())

	n.Set(2, 1)
	fmt.Println(m.Map())

}
Output:

map[1:1]
map[2:2]
map[2:1]

func (*IntIntMap) Search

func (m *IntIntMap) Search(key int) (value int, found bool)

Search searches the map with given `key`. Second return parameter `found` is true if key was found, otherwise false.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewIntIntMap()

	m.Set(1, 1)

	value, found := m.Search(1)
	if found {
		fmt.Println("find key1 value:", value)
	}

	value, found = m.Search(2)
	if !found {
		fmt.Println("key2 not find")
	}

}
Output:

find key1 value: 1
key2 not find

func (*IntIntMap) Set

func (m *IntIntMap) Set(key int, val int)

Set sets key-value to the hash map.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewIntIntMap()

	m.Set(1, 1)
	fmt.Println(m)

}
Output:

{"1":1}

func (*IntIntMap) SetIfNotExist

func (m *IntIntMap) SetIfNotExist(key int, value int) bool

SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true. It returns false if `key` exists, and `value` would be ignored.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.IntIntMap
	fmt.Println(m.SetIfNotExist(1, 1))
	fmt.Println(m.SetIfNotExist(1, 2))
	fmt.Println(m.Map())

}
Output:

true
false
map[1:1]

func (*IntIntMap) SetIfNotExistFunc

func (m *IntIntMap) SetIfNotExistFunc(key int, f func() int) bool

SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and `value` would be ignored.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.IntIntMap
	fmt.Println(m.SetIfNotExistFunc(1, func() int {
		return 1
	}))
	fmt.Println(m.SetIfNotExistFunc(1, func() int {
		return 2
	}))
	fmt.Println(m.Map())

}
Output:

true
false
map[1:1]

func (*IntIntMap) SetIfNotExistFuncLock

func (m *IntIntMap) SetIfNotExistFuncLock(key int, f func() int) bool

SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and `value` would be ignored.

SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that it executes function `f` with mutex.Lock of the hash map.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.IntIntMap
	fmt.Println(m.SetIfNotExistFuncLock(1, func() int {
		return 1
	}))
	fmt.Println(m.SetIfNotExistFuncLock(1, func() int {
		return 2
	}))
	fmt.Println(m.Map())

}
Output:

true
false
map[1:1]

func (*IntIntMap) Sets

func (m *IntIntMap) Sets(data map[int]int)

Sets batch sets key-values to the hash map.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewIntIntMap()

	addMap := make(map[int]int)
	addMap[1] = 1
	addMap[2] = 12
	addMap[3] = 123

	m.Sets(addMap)
	fmt.Println(m)

}
Output:

{"1":1,"2":12,"3":123}

func (*IntIntMap) Size

func (m *IntIntMap) Size() int

Size returns the size of the map.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.IntIntMap
	m.Sets(g.MapIntInt{
		1: 1,
		2: 2,
		3: 3,
		4: 4,
	})

	fmt.Println(m.Size())

}
Output:

4

func (*IntIntMap) String

func (m *IntIntMap) String() string

String returns the map as a string.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.IntIntMap
	m.Sets(g.MapIntInt{
		1: 1,
	})

	fmt.Println(m.String())

}
Output:

{"1":1}

func (*IntIntMap) UnmarshalJSON

func (m *IntIntMap) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/internal/json"
	"github.com/gogf/gf/v2/util/gconv"
)

func main() {
	var m gmap.IntIntMap
	m.Sets(g.MapIntInt{
		1: 1,
		2: 2,
		3: 3,
		4: 4,
	})

	var n gmap.Map

	err := json.Unmarshal(gconv.Bytes(m.String()), &n)
	if err == nil {
		fmt.Println(n.Map())
	}

}
Output:

map[1:1 2:2 3:3 4:4]

func (*IntIntMap) UnmarshalValue

func (m *IntIntMap) UnmarshalValue(value interface{}) (err error)

UnmarshalValue is an interface implement which sets any type of value for map.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/util/gconv"
)

func main() {
	var m gmap.IntIntMap

	n := map[int]int{
		1: 1001,
		2: 1002,
		3: 1003,
	}

	if err := gconv.Scan(n, &m); err == nil {
		fmt.Printf("%#v", m.Map())
	}
}
Output:

map[int]int{1:1001, 2:1002, 3:1003}

func (*IntIntMap) Values

func (m *IntIntMap) Values() []int

Values returns all values of the map as a slice.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.IntIntMap
	m.Sets(g.MapIntInt{
		1: 1,
		2: 2,
		3: 3,
		4: 4,
	})
	fmt.Println(m.Values())

	// May Output:
	// [1 v2 v3 4]
}
Output:

type IntStrMap

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

func NewIntStrMap

func NewIntStrMap(safe ...bool) *IntStrMap

NewIntStrMap returns an empty IntStrMap object. The parameter `safe` is used to specify whether using map in concurrent-safety, which is false in default.

func NewIntStrMapFrom

func NewIntStrMapFrom(data map[int]string, safe ...bool) *IntStrMap

NewIntStrMapFrom creates and returns a hash map from given map `data`. Note that, the param `data` map will be set as the underlying data map(no deep copy), there might be some concurrent-safe issues when changing the map outside.

func (*IntStrMap) Clear

func (m *IntStrMap) Clear()

Clear deletes all data of the map, it will remake a new underlying data map.

func (*IntStrMap) Clone

func (m *IntStrMap) Clone() *IntStrMap

Clone returns a new hash map with copy of current map data.

func (*IntStrMap) Contains

func (m *IntStrMap) Contains(key int) bool

Contains checks whether a key exists. It returns true if the `key` exists, or else false.

func (*IntStrMap) FilterEmpty

func (m *IntStrMap) FilterEmpty()

FilterEmpty deletes all key-value pair of which the value is empty. Values like: 0, nil, false, "", len(slice/map/chan) == 0 are considered empty.

func (*IntStrMap) Flip

func (m *IntStrMap) Flip()

Flip exchanges key-value of the map to value-key.

func (*IntStrMap) Get

func (m *IntStrMap) Get(key int) (value string)

Get returns the value by given `key`.

func (*IntStrMap) GetOrSet

func (m *IntStrMap) GetOrSet(key int, value string) string

GetOrSet returns the value by key, or sets value with given `value` if it does not exist and then returns this value.

func (*IntStrMap) GetOrSetFunc

func (m *IntStrMap) GetOrSetFunc(key int, f func() string) string

GetOrSetFunc returns the value by key, or sets value with returned value of callback function `f` if it does not exist and returns this value.

func (*IntStrMap) GetOrSetFuncLock

func (m *IntStrMap) GetOrSetFuncLock(key int, f func() string) string

GetOrSetFuncLock returns the value by key, or sets value with returned value of callback function `f` if it does not exist and returns this value.

GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` with mutex.Lock of the hash map.

func (*IntStrMap) IsEmpty

func (m *IntStrMap) IsEmpty() bool

IsEmpty checks whether the map is empty. It returns true if map is empty, or else false.

func (*IntStrMap) Iterator

func (m *IntStrMap) Iterator(f func(k int, v string) bool)

Iterator iterates the hash map readonly with custom callback function `f`. If `f` returns true, then it continues iterating; or false to stop.

func (*IntStrMap) Keys

func (m *IntStrMap) Keys() []int

Keys returns all keys of the map as a slice.

func (*IntStrMap) LockFunc

func (m *IntStrMap) LockFunc(f func(m map[int]string))

LockFunc locks writing with given callback function `f` within RWMutex.Lock.

func (*IntStrMap) Map

func (m *IntStrMap) Map() map[int]string

Map returns the underlying data map. Note that, if it's in concurrent-safe usage, it returns a copy of underlying data, or else a pointer to the underlying data.

func (*IntStrMap) MapCopy

func (m *IntStrMap) MapCopy() map[int]string

MapCopy returns a copy of the underlying data of the hash map.

func (*IntStrMap) MapStrAny

func (m *IntStrMap) MapStrAny() map[string]interface{}

MapStrAny returns a copy of the underlying data of the map as map[string]interface{}.

func (IntStrMap) MarshalJSON

func (m IntStrMap) MarshalJSON() ([]byte, error)

MarshalJSON implements the interface MarshalJSON for json.Marshal.

func (*IntStrMap) Merge

func (m *IntStrMap) Merge(other *IntStrMap)

Merge merges two hash maps. The `other` map will be merged into the map `m`.

func (*IntStrMap) Pop

func (m *IntStrMap) Pop() (key int, value string)

Pop retrieves and deletes an item from the map.

func (*IntStrMap) Pops

func (m *IntStrMap) Pops(size int) map[int]string

Pops retrieves and deletes `size` items from the map. It returns all items if size == -1.

func (*IntStrMap) RLockFunc

func (m *IntStrMap) RLockFunc(f func(m map[int]string))

RLockFunc locks reading with given callback function `f` within RWMutex.RLock.

func (*IntStrMap) Remove

func (m *IntStrMap) Remove(key int) (value string)

Remove deletes value from map by given `key`, and return this deleted value.

func (*IntStrMap) Removes

func (m *IntStrMap) Removes(keys []int)

Removes batch deletes values of the map by keys.

func (*IntStrMap) Replace

func (m *IntStrMap) Replace(data map[int]string)

Replace the data of the map with given `data`.

func (*IntStrMap) Search

func (m *IntStrMap) Search(key int) (value string, found bool)

Search searches the map with given `key`. Second return parameter `found` is true if key was found, otherwise false.

func (*IntStrMap) Set

func (m *IntStrMap) Set(key int, val string)

Set sets key-value to the hash map.

func (*IntStrMap) SetIfNotExist

func (m *IntStrMap) SetIfNotExist(key int, value string) bool

SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true. It returns false if `key` exists, and `value` would be ignored.

func (*IntStrMap) SetIfNotExistFunc

func (m *IntStrMap) SetIfNotExistFunc(key int, f func() string) bool

SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and `value` would be ignored.

func (*IntStrMap) SetIfNotExistFuncLock

func (m *IntStrMap) SetIfNotExistFuncLock(key int, f func() string) bool

SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and `value` would be ignored.

SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that it executes function `f` with mutex.Lock of the hash map.

func (*IntStrMap) Sets

func (m *IntStrMap) Sets(data map[int]string)

Sets batch sets key-values to the hash map.

func (*IntStrMap) Size

func (m *IntStrMap) Size() int

Size returns the size of the map.

func (*IntStrMap) String

func (m *IntStrMap) String() string

String returns the map as a string.

func (*IntStrMap) UnmarshalJSON

func (m *IntStrMap) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.

func (*IntStrMap) UnmarshalValue

func (m *IntStrMap) UnmarshalValue(value interface{}) (err error)

UnmarshalValue is an interface implement which sets any type of value for map.

func (*IntStrMap) Values

func (m *IntStrMap) Values() []string

Values returns all values of the map as a slice.

type ListMap

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

ListMap is a map that preserves insertion-order.

It is backed by a hash table to store values and doubly-linked list to store ordering.

Structure is not thread safe.

Reference: http://en.wikipedia.org/wiki/Associative_array

func NewListMap

func NewListMap(safe ...bool) *ListMap

NewListMap returns an empty link map. ListMap is backed by a hash table to store values and doubly-linked list to store ordering. The parameter `safe` is used to specify whether using map in concurrent-safety, which is false in default.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewListMap()

	m.Set("key1", "var1")
	m.Set("key2", "var2")
	fmt.Println(m)

}
Output:

{"key1":"var1","key2":"var2"}

func NewListMapFrom

func NewListMapFrom(data map[interface{}]interface{}, safe ...bool) *ListMap

NewListMapFrom returns a link map from given map `data`. Note that, the param `data` map will be set as the underlying data map(no deep copy), there might be some concurrent-safe issues when changing the map outside.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewListMap()

	m.Set("key1", "var1")
	m.Set("key2", "var2")
	fmt.Println(m)

	n := gmap.NewListMapFrom(m.Map(), true)
	fmt.Println(n)

	// May Output:
	// {"key1":"var1","key2":"var2"}
	// {"key1":"var1","key2":"var2"}
}
Output:

func (*ListMap) Clear

func (m *ListMap) Clear()

Clear deletes all data of the map, it will remake a new underlying data map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.ListMap
	m.Sets(g.MapAnyAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})

	m.Clear()

	fmt.Println(m.Map())

}
Output:

map[]

func (*ListMap) Clone

func (m *ListMap) Clone(safe ...bool) *ListMap

Clone returns a new link map with copy of current map data.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewListMap()

	m.Set("key1", "val1")
	fmt.Println(m)

	n := m.Clone()
	fmt.Println(n)

}
Output:

{"key1":"val1"}
{"key1":"val1"}

func (*ListMap) Contains

func (m *ListMap) Contains(key interface{}) (ok bool)

Contains checks whether a key exists. It returns true if the `key` exists, or else false.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.ListMap
	m.Sets(g.MapAnyAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})

	fmt.Println(m.Contains("k1"))
	fmt.Println(m.Contains("k5"))

}
Output:

true
false

func (*ListMap) FilterEmpty

func (m *ListMap) FilterEmpty()

FilterEmpty deletes all key-value pair of which the value is empty.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	m := gmap.NewListMapFrom(g.MapAnyAny{
		"k1": "",
		"k2": nil,
		"k3": 0,
		"k4": 1,
	})
	m.FilterEmpty()
	fmt.Println(m.Map())

}
Output:

map[k4:1]

func (*ListMap) Flip

func (m *ListMap) Flip()

Flip exchanges key-value of the map to value-key.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.ListMap
	m.Sets(g.MapAnyAny{
		"k1": "v1",
	})
	m.Flip()
	fmt.Println(m.Map())

}
Output:

map[v1:k1]

func (*ListMap) Get

func (m *ListMap) Get(key interface{}) (value interface{})

Get returns the value by given `key`.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewListMap()

	m.Set("key1", "val1")

	fmt.Println("key1 value:", m.Get("key1"))
	fmt.Println("key2 value:", m.Get("key2"))

}
Output:

key1 value: val1
key2 value: <nil>

func (*ListMap) GetOrSet

func (m *ListMap) GetOrSet(key interface{}, value interface{}) interface{}

GetOrSet returns the value by key, or sets value with given `value` if it does not exist and then returns this value.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewListMap()
	m.Set("key1", "val1")

	fmt.Println(m.GetOrSet("key1", "NotExistValue"))
	fmt.Println(m.GetOrSet("key2", "val2"))

}
Output:

val1
val2

func (*ListMap) GetOrSetFunc

func (m *ListMap) GetOrSetFunc(key interface{}, f func() interface{}) interface{}

GetOrSetFunc returns the value by key, or sets value with returned value of callback function `f` if it does not exist and then returns this value.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewListMap()
	m.Set("key1", "val1")

	fmt.Println(m.GetOrSetFunc("key1", func() interface{} {
		return "NotExistValue"
	}))
	fmt.Println(m.GetOrSetFunc("key2", func() interface{} {
		return "NotExistValue"
	}))

}
Output:

val1
NotExistValue

func (*ListMap) GetOrSetFuncLock

func (m *ListMap) GetOrSetFuncLock(key interface{}, f func() interface{}) interface{}

GetOrSetFuncLock returns the value by key, or sets value with returned value of callback function `f` if it does not exist and then returns this value.

GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` with mutex.Lock of the map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewListMap()
	m.Set("key1", "val1")

	fmt.Println(m.GetOrSetFuncLock("key1", func() interface{} {
		return "NotExistValue"
	}))
	fmt.Println(m.GetOrSetFuncLock("key2", func() interface{} {
		return "NotExistValue"
	}))

}
Output:

val1
NotExistValue

func (*ListMap) GetVar

func (m *ListMap) GetVar(key interface{}) *gvar.Var

GetVar returns a Var with the value by given `key`. The returned Var is un-concurrent safe.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewListMap()
	m.Set("key1", "val1")

	fmt.Println(m.GetVar("key1"))
	fmt.Println(m.GetVar("key2").IsNil())

}
Output:

val1
true

func (*ListMap) GetVarOrSet

func (m *ListMap) GetVarOrSet(key interface{}, value interface{}) *gvar.Var

GetVarOrSet returns a Var with result from GetVarOrSet. The returned Var is un-concurrent safe.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewListMap()
	m.Set("key1", "val1")

	fmt.Println(m.GetVarOrSet("key1", "NotExistValue"))
	fmt.Println(m.GetVarOrSet("key2", "val2"))

}
Output:

val1
val2

func (*ListMap) GetVarOrSetFunc

func (m *ListMap) GetVarOrSetFunc(key interface{}, f func() interface{}) *gvar.Var

GetVarOrSetFunc returns a Var with result from GetOrSetFunc. The returned Var is un-concurrent safe.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewListMap()
	m.Set("key1", "val1")

	fmt.Println(m.GetVarOrSetFunc("key1", func() interface{} {
		return "NotExistValue"
	}))
	fmt.Println(m.GetVarOrSetFunc("key2", func() interface{} {
		return "NotExistValue"
	}))

}
Output:

val1
NotExistValue

func (*ListMap) GetVarOrSetFuncLock

func (m *ListMap) GetVarOrSetFuncLock(key interface{}, f func() interface{}) *gvar.Var

GetVarOrSetFuncLock returns a Var with result from GetOrSetFuncLock. The returned Var is un-concurrent safe.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewListMap()
	m.Set("key1", "val1")

	fmt.Println(m.GetVarOrSetFuncLock("key1", func() interface{} {
		return "NotExistValue"
	}))
	fmt.Println(m.GetVarOrSetFuncLock("key2", func() interface{} {
		return "NotExistValue"
	}))

}
Output:

val1
NotExistValue

func (*ListMap) IsEmpty

func (m *ListMap) IsEmpty() bool

IsEmpty checks whether the map is empty. It returns true if map is empty, or else false.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.ListMap
	fmt.Println(m.IsEmpty())

	m.Set("k1", "v1")
	fmt.Println(m.IsEmpty())

}
Output:

true
false

func (*ListMap) Iterator

func (m *ListMap) Iterator(f func(key, value interface{}) bool)

Iterator is alias of IteratorAsc.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewListMap()
	for i := 0; i < 10; i++ {
		m.Set(i, i*2)
	}

	var totalKey, totalValue int
	m.Iterator(func(k interface{}, v interface{}) bool {
		totalKey += k.(int)
		totalValue += v.(int)

		return totalKey < 10
	})

	fmt.Println("totalKey:", totalKey)
	fmt.Println("totalValue:", totalValue)

}
Output:

totalKey: 10
totalValue: 20

func (*ListMap) IteratorAsc

func (m *ListMap) IteratorAsc(f func(key interface{}, value interface{}) bool)

IteratorAsc iterates the map readonly in ascending order with given callback function `f`. If `f` returns true, then it continues iterating; or false to stop.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewListMap()
	for i := 0; i < 10; i++ {
		m.Set(i, i*2)
	}

	var totalKey, totalValue int
	m.IteratorAsc(func(k interface{}, v interface{}) bool {
		totalKey += k.(int)
		totalValue += v.(int)

		return totalKey < 10
	})

	fmt.Println("totalKey:", totalKey)
	fmt.Println("totalValue:", totalValue)

}
Output:

totalKey: 10
totalValue: 20

func (*ListMap) IteratorDesc

func (m *ListMap) IteratorDesc(f func(key interface{}, value interface{}) bool)

IteratorDesc iterates the map readonly in descending order with given callback function `f`. If `f` returns true, then it continues iterating; or false to stop.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewListMap()
	for i := 0; i < 10; i++ {
		m.Set(i, i*2)
	}

	var totalKey, totalValue int
	m.IteratorDesc(func(k interface{}, v interface{}) bool {
		totalKey += k.(int)
		totalValue += v.(int)

		return totalKey < 10
	})

	fmt.Println("totalKey:", totalKey)
	fmt.Println("totalValue:", totalValue)

}
Output:

totalKey: 17
totalValue: 34

func (*ListMap) Keys

func (m *ListMap) Keys() []interface{}

Keys returns all keys of the map as a slice in ascending order.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.ListMap
	m.Sets(g.MapAnyAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})
	fmt.Println(m.Keys())

	// May Output:
	// [k1 k2 k3 k4]
}
Output:

func (*ListMap) Map

func (m *ListMap) Map() map[interface{}]interface{}

Map returns a copy of the underlying data of the map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m1 := gmap.NewListMap()
	m1.Set("key1", "val1")
	fmt.Println("m1:", m1)

	n1 := m1.Map()
	fmt.Println("before n1:", n1)
	m1.Set("key1", "val2")
	fmt.Println("after n1:", n1)

}
Output:

m1: {"key1":"val1"}
before n1: map[key1:val1]
after n1: map[key1:val1]

func (*ListMap) MapStrAny

func (m *ListMap) MapStrAny() map[string]interface{}

MapStrAny returns a copy of the underlying data of the map as map[string]interface{}.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewListMap()
	m.Set("key1", "val1")
	m.Set("key2", "val2")

	n := m.MapStrAny()
	fmt.Printf("%#v", n)

}
Output:

map[string]interface {}{"key1":"val1", "key2":"val2"}

func (ListMap) MarshalJSON

func (m ListMap) MarshalJSON() (jsonBytes []byte, err error)

MarshalJSON implements the interface MarshalJSON for json.Marshal.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/internal/json"
	"github.com/gogf/gf/v2/util/gconv"
)

func main() {
	var m gmap.ListMap
	m.Set("k1", "v1")
	m.Set("k2", "v2")
	m.Set("k3", "v3")
	m.Set("k4", "v4")

	bytes, err := json.Marshal(&m)
	if err == nil {
		fmt.Println(gconv.String(bytes))
	}

}
Output:

{"k1":"v1","k2":"v2","k3":"v3","k4":"v4"}

func (*ListMap) Merge

func (m *ListMap) Merge(other *ListMap)

Merge merges two link maps. The `other` map will be merged into the map `m`.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m1, m2 gmap.ListMap
	m1.Set("key1", "val1")
	m2.Set("key2", "val2")
	m1.Merge(&m2)
	fmt.Println(m1.Map())

	// May Output:
	// map[key1:val1 key2:val2]
}
Output:

func (*ListMap) Pop

func (m *ListMap) Pop() (key, value interface{})

Pop retrieves and deletes an item from the map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.ListMap
	m.Sets(g.MapAnyAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})

	fmt.Println(m.Pop())

	// May Output:
	// k1 v1
}
Output:

func (*ListMap) Pops

func (m *ListMap) Pops(size int) map[interface{}]interface{}

Pops retrieves and deletes `size` items from the map. It returns all items if size == -1.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.ListMap
	m.Sets(g.MapAnyAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})
	fmt.Println(m.Pops(-1))
	fmt.Println("size:", m.Size())

	m.Sets(g.MapAnyAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})
	fmt.Println(m.Pops(2))
	fmt.Println("size:", m.Size())

	// May Output:
	// map[k1:v1 k2:v2 k3:v3 k4:v4]
	// size: 0
	// map[k1:v1 k2:v2]
	// size: 2
}
Output:

func (*ListMap) Remove

func (m *ListMap) Remove(key interface{}) (value interface{})

Remove deletes value from map by given `key`, and return this deleted value.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.ListMap
	m.Set("k1", "v1")

	fmt.Println(m.Remove("k1"))
	fmt.Println(m.Remove("k2"))
	fmt.Println(m.Size())

}
Output:

v1
<nil>
0

func (*ListMap) Removes

func (m *ListMap) Removes(keys []interface{})

Removes batch deletes values of the map by keys.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.ListMap
	m.Sets(g.MapAnyAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})

	removeList := make([]interface{}, 2)
	removeList = append(removeList, "k1")
	removeList = append(removeList, "k2")

	m.Removes(removeList)

	fmt.Println(m.Map())

}
Output:

map[k3:v3 k4:v4]

func (*ListMap) Replace

func (m *ListMap) Replace(data map[interface{}]interface{})

Replace the data of the map with given `data`.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.ListMap
	m.Sets(g.MapAnyAny{
		"k1": "v1",
	})

	var n gmap.ListMap
	n.Sets(g.MapAnyAny{
		"k2": "v2",
	})

	fmt.Println(m.Map())

	m.Replace(n.Map())
	fmt.Println(m.Map())

}
Output:

map[k1:v1]
map[k2:v2]

func (*ListMap) Search

func (m *ListMap) Search(key interface{}) (value interface{}, found bool)

Search searches the map with given `key`. Second return parameter `found` is true if key was found, otherwise false.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewListMap()

	m.Set("key1", "val1")

	value, found := m.Search("key1")
	if found {
		fmt.Println("find key1 value:", value)
	}

	value, found = m.Search("key2")
	if !found {
		fmt.Println("key2 not find")
	}

}
Output:

find key1 value: val1
key2 not find

func (*ListMap) Set

func (m *ListMap) Set(key interface{}, value interface{})

Set sets key-value to the map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewListMap()

	m.Set("key1", "val1")
	fmt.Println(m)

}
Output:

{"key1":"val1"}

func (*ListMap) SetIfNotExist

func (m *ListMap) SetIfNotExist(key interface{}, value interface{}) bool

SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true. It returns false if `key` exists, and `value` would be ignored.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.ListMap
	fmt.Println(m.SetIfNotExist("k1", "v1"))
	fmt.Println(m.SetIfNotExist("k1", "v2"))
	fmt.Println(m.Map())

}
Output:

true
false
map[k1:v1]

func (*ListMap) SetIfNotExistFunc

func (m *ListMap) SetIfNotExistFunc(key interface{}, f func() interface{}) bool

SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and `value` would be ignored.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.ListMap
	fmt.Println(m.SetIfNotExistFunc("k1", func() interface{} {
		return "v1"
	}))
	fmt.Println(m.SetIfNotExistFunc("k1", func() interface{} {
		return "v2"
	}))
	fmt.Println(m.Map())

}
Output:

true
false
map[k1:v1]

func (*ListMap) SetIfNotExistFuncLock

func (m *ListMap) SetIfNotExistFuncLock(key interface{}, f func() interface{}) bool

SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and `value` would be ignored.

SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that it executes function `f` with mutex.Lock of the map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.ListMap
	fmt.Println(m.SetIfNotExistFuncLock("k1", func() interface{} {
		return "v1"
	}))
	fmt.Println(m.SetIfNotExistFuncLock("k1", func() interface{} {
		return "v2"
	}))
	fmt.Println(m.Map())

}
Output:

true
false
map[k1:v1]

func (*ListMap) Sets

func (m *ListMap) Sets(data map[interface{}]interface{})

Sets batch sets key-values to the map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewListMap()

	addMap := make(map[interface{}]interface{})
	addMap["key1"] = "val1"
	addMap["key2"] = "val2"
	addMap["key3"] = "val3"

	m.Sets(addMap)
	fmt.Println(m)

	// May Output:
	// {"key1":"val1","key2":"val2","key3":"val3"}
}
Output:

func (*ListMap) Size

func (m *ListMap) Size() (size int)

Size returns the size of the map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.ListMap
	m.Sets(g.MapAnyAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})

	fmt.Println(m.Size())

}
Output:

4

func (*ListMap) String

func (m *ListMap) String() string

String returns the map as a string.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.ListMap
	m.Sets(g.MapAnyAny{
		"k1": "v1",
	})

	fmt.Println(m.String())

}
Output:

{"k1":"v1"}

func (*ListMap) UnmarshalJSON

func (m *ListMap) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/internal/json"
	"github.com/gogf/gf/v2/util/gconv"
)

func main() {
	var m gmap.ListMap
	m.Sets(g.MapAnyAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})

	var n gmap.ListMap

	err := json.Unmarshal(gconv.Bytes(m.String()), &n)
	if err == nil {
		fmt.Println(n.Map())
	}

}
Output:

map[k1:v1 k2:v2 k3:v3 k4:v4]

func (*ListMap) UnmarshalValue

func (m *ListMap) UnmarshalValue(value interface{}) (err error)

UnmarshalValue is an interface implement which sets any type of value for map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/util/gconv"
)

func main() {
	type User struct {
		Uid   int
		Name  string
		Pass1 string `gconv:"password1"`
		Pass2 string `gconv:"password2"`
	}

	var (
		m    gmap.AnyAnyMap
		user = User{
			Uid:   1,
			Name:  "john",
			Pass1: "123",
			Pass2: "456",
		}
	)
	if err := gconv.Scan(user, &m); err == nil {
		fmt.Printf("%#v", m.Map())
	}

}
Output:

map[interface {}]interface {}{"Name":"john", "Uid":1, "password1":"123", "password2":"456"}

func (*ListMap) Values

func (m *ListMap) Values() []interface{}

Values returns all values of the map as a slice.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.ListMap
	m.Sets(g.MapAnyAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})
	fmt.Println(m.Values())

	// May Output:
	// [v1 v2 v3 v4]
}
Output:

type Map

type Map = AnyAnyMap // Map is alias of AnyAnyMap.

func New

func New(safe ...bool) *Map

New creates and returns an empty hash map. The parameter `safe` is used to specify whether using map in concurrent-safety, which is false in default.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.New()

	// Add data.
	m.Set("key1", "val1")

	// Print size.
	fmt.Println(m.Size())

	addMap := make(map[interface{}]interface{})
	addMap["key2"] = "val2"
	addMap["key3"] = "val3"
	addMap[1] = 1

	fmt.Println(m.Values())

	// Batch add data.
	m.Sets(addMap)

	// Gets the value of the corresponding key.
	fmt.Println(m.Get("key3"))

	// Get the value by key, or set it with given key-value if not exist.
	fmt.Println(m.GetOrSet("key4", "val4"))

	// Set key-value if the key does not exist, then return true; or else return false.
	fmt.Println(m.SetIfNotExist("key3", "val3"))

	// Remove key
	m.Remove("key2")
	fmt.Println(m.Keys())

	// Batch remove keys.
	m.Removes([]interface{}{"key1", 1})
	fmt.Println(m.Keys())

	// Contains checks whether a key exists.
	fmt.Println(m.Contains("key3"))

	// Flip exchanges key-value of the map, it will change key-value to value-key.
	m.Flip()
	fmt.Println(m.Map())

	// Clear deletes all data of the map.
	m.Clear()

	fmt.Println(m.Size())

	// May Output:
	// 1
	// [val1]
	// val3
	// val4
	// false
	// [key4 key1 key3 1]
	// [key4 key3]
	// true
	// map[val3:key3 val4:key4]
	// 0
}
Output:

func NewFrom

func NewFrom(data map[interface{}]interface{}, safe ...bool) *Map

NewFrom creates and returns a hash map from given map `data`. Note that, the param `data` map will be set as the underlying data map(no deep copy), there might be some concurrent-safe issues when changing the map outside. The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.New()

	m.Set("key1", "val1")
	fmt.Println(m)

	n := gmap.NewFrom(m.MapCopy(), true)
	fmt.Println(n)

}
Output:

{"key1":"val1"}
{"key1":"val1"}

func NewHashMap

func NewHashMap(safe ...bool) *Map

NewHashMap creates and returns an empty hash map. The parameter `safe` is used to specify whether using map in concurrent-safety, which is false in default.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.New()

	m.Set("key1", "val1")
	fmt.Println(m)

}
Output:

{"key1":"val1"}

func NewHashMapFrom

func NewHashMapFrom(data map[interface{}]interface{}, safe ...bool) *Map

NewHashMapFrom creates and returns a hash map from given map `data`. Note that, the param `data` map will be set as the underlying data map(no deep copy), there might be some concurrent-safe issues when changing the map outside. The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.New()

	m.Set("key1", "val1")
	fmt.Println(m)

	n := gmap.NewFrom(m.MapCopy(), true)
	fmt.Println(n)

}
Output:

{"key1":"val1"}
{"key1":"val1"}

type StrAnyMap

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

func NewStrAnyMap

func NewStrAnyMap(safe ...bool) *StrAnyMap

NewStrAnyMap returns an empty StrAnyMap object. The parameter `safe` is used to specify whether using map in concurrent-safety, which is false in default.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrAnyMap()

	m.Set("key1", "var1")
	fmt.Println(m)

}
Output:

{"key1":"var1"}

func NewStrAnyMapFrom

func NewStrAnyMapFrom(data map[string]interface{}, safe ...bool) *StrAnyMap

NewStrAnyMapFrom creates and returns a hash map from given map `data`. Note that, the param `data` map will be set as the underlying data map(no deep copy), there might be some concurrent-safe issues when changing the map outside.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrAnyMap()

	m.Set("key1", "var1")
	fmt.Println(m)

	n := gmap.NewStrAnyMapFrom(m.MapCopy(), true)
	fmt.Println(n)

}
Output:

{"key1":"var1"}
{"key1":"var1"}

func (*StrAnyMap) Clear

func (m *StrAnyMap) Clear()

Clear deletes all data of the map, it will remake a new underlying data map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.StrAnyMap
	m.Sets(g.MapStrAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})

	m.Clear()

	fmt.Println(m.Map())

}
Output:

map[]

func (*StrAnyMap) Clone

func (m *StrAnyMap) Clone() *StrAnyMap

Clone returns a new hash map with copy of current map data.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrAnyMap()

	m.Set("key1", "val1")
	fmt.Println(m)

	n := m.Clone()
	fmt.Println(n)

}
Output:

{"key1":"val1"}
{"key1":"val1"}

func (*StrAnyMap) Contains

func (m *StrAnyMap) Contains(key string) bool

Contains checks whether a key exists. It returns true if the `key` exists, or else false.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.StrAnyMap
	m.Sets(g.MapStrAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})

	fmt.Println(m.Contains("k1"))
	fmt.Println(m.Contains("k5"))

}
Output:

true
false

func (*StrAnyMap) FilterEmpty

func (m *StrAnyMap) FilterEmpty()

FilterEmpty deletes all key-value pair of which the value is empty. Values like: 0, nil, false, "", len(slice/map/chan) == 0 are considered empty.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	m := gmap.NewStrAnyMapFrom(g.MapStrAny{
		"k1": "",
		"k2": nil,
		"k3": 0,
		"k4": 1,
	})
	m.FilterEmpty()
	fmt.Println(m.Map())

}
Output:

map[k4:1]

func (*StrAnyMap) FilterNil

func (m *StrAnyMap) FilterNil()

FilterNil deletes all key-value pair of which the value is nil.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	m := gmap.NewStrAnyMapFrom(g.MapStrAny{
		"k1": "",
		"k2": nil,
		"k3": 0,
		"k4": 1,
	})
	m.FilterNil()
	fmt.Printf("%#v", m.Map())

}
Output:

map[string]interface {}{"k1":"", "k3":0, "k4":1}

func (*StrAnyMap) Flip

func (m *StrAnyMap) Flip()

Flip exchanges key-value of the map to value-key.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.StrAnyMap
	m.Sets(g.MapStrAny{
		"k1": "v1",
	})
	m.Flip()
	fmt.Println(m.Map())

}
Output:

map[v1:k1]

func (*StrAnyMap) Get

func (m *StrAnyMap) Get(key string) (value interface{})

Get returns the value by given `key`.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrAnyMap()

	m.Set("key1", "val1")

	fmt.Println("key1 value:", m.Get("key1"))
	fmt.Println("key2 value:", m.Get("key2"))

}
Output:

key1 value: val1
key2 value: <nil>

func (*StrAnyMap) GetOrSet

func (m *StrAnyMap) GetOrSet(key string, value interface{}) interface{}

GetOrSet returns the value by key, or sets value with given `value` if it does not exist and then returns this value.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrAnyMap()
	m.Set("key1", "val1")

	fmt.Println(m.GetOrSet("key1", "NotExistValue"))
	fmt.Println(m.GetOrSet("key2", "val2"))

}
Output:

val1
val2

func (*StrAnyMap) GetOrSetFunc

func (m *StrAnyMap) GetOrSetFunc(key string, f func() interface{}) interface{}

GetOrSetFunc returns the value by key, or sets value with returned value of callback function `f` if it does not exist and then returns this value.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrAnyMap()
	m.Set("key1", "val1")

	fmt.Println(m.GetOrSetFunc("key1", func() interface{} {
		return "NotExistValue"
	}))
	fmt.Println(m.GetOrSetFunc("key2", func() interface{} {
		return "NotExistValue"
	}))

}
Output:

val1
NotExistValue

func (*StrAnyMap) GetOrSetFuncLock

func (m *StrAnyMap) GetOrSetFuncLock(key string, f func() interface{}) interface{}

GetOrSetFuncLock returns the value by key, or sets value with returned value of callback function `f` if it does not exist and then returns this value.

GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` with mutex.Lock of the hash map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrAnyMap()
	m.Set("key1", "val1")

	fmt.Println(m.GetOrSetFuncLock("key1", func() interface{} {
		return "NotExistValue"
	}))
	fmt.Println(m.GetOrSetFuncLock("key2", func() interface{} {
		return "NotExistValue"
	}))

}
Output:

val1
NotExistValue

func (*StrAnyMap) GetVar

func (m *StrAnyMap) GetVar(key string) *gvar.Var

GetVar returns a Var with the value by given `key`. The returned Var is un-concurrent safe.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrAnyMap()
	m.Set("key1", "val1")

	fmt.Println(m.GetVar("key1"))
	fmt.Println(m.GetVar("key2").IsNil())

}
Output:

val1
true

func (*StrAnyMap) GetVarOrSet

func (m *StrAnyMap) GetVarOrSet(key string, value interface{}) *gvar.Var

GetVarOrSet returns a Var with result from GetVarOrSet. The returned Var is un-concurrent safe.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrAnyMap()
	m.Set("key1", "val1")

	fmt.Println(m.GetVarOrSet("key1", "NotExistValue"))
	fmt.Println(m.GetVarOrSet("key2", "val2"))

}
Output:

val1
val2

func (*StrAnyMap) GetVarOrSetFunc

func (m *StrAnyMap) GetVarOrSetFunc(key string, f func() interface{}) *gvar.Var

GetVarOrSetFunc returns a Var with result from GetOrSetFunc. The returned Var is un-concurrent safe.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrAnyMap()
	m.Set("key1", "val1")

	fmt.Println(m.GetVarOrSetFunc("key1", func() interface{} {
		return "NotExistValue"
	}))
	fmt.Println(m.GetVarOrSetFunc("key2", func() interface{} {
		return "NotExistValue"
	}))

}
Output:

val1
NotExistValue

func (*StrAnyMap) GetVarOrSetFuncLock

func (m *StrAnyMap) GetVarOrSetFuncLock(key string, f func() interface{}) *gvar.Var

GetVarOrSetFuncLock returns a Var with result from GetOrSetFuncLock. The returned Var is un-concurrent safe.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrAnyMap()
	m.Set("key1", "val1")

	fmt.Println(m.GetVarOrSetFuncLock("key1", func() interface{} {
		return "NotExistValue"
	}))
	fmt.Println(m.GetVarOrSetFuncLock("key2", func() interface{} {
		return "NotExistValue"
	}))

}
Output:

val1
NotExistValue

func (*StrAnyMap) IsEmpty

func (m *StrAnyMap) IsEmpty() bool

IsEmpty checks whether the map is empty. It returns true if map is empty, or else false.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrAnyMap
	fmt.Println(m.IsEmpty())

	m.Set("k1", "v1")
	fmt.Println(m.IsEmpty())

}
Output:

true
false

func (*StrAnyMap) Iterator

func (m *StrAnyMap) Iterator(f func(k string, v interface{}) bool)

Iterator iterates the hash map readonly with custom callback function `f`. If `f` returns true, then it continues iterating; or false to stop.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/util/gconv"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrAnyMap()
	for i := 1; i <= 10; i++ {
		m.Set(gconv.String(i), i*2)
	}

	var totalValue int
	m.Iterator(func(k string, v interface{}) bool {
		totalValue += v.(int)

		return totalValue < 50
	})

	fmt.Println("totalValue:", totalValue)

	// May Output:
	// totalValue: 52
}
Output:

func (*StrAnyMap) Keys

func (m *StrAnyMap) Keys() []string

Keys returns all keys of the map as a slice.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.StrAnyMap
	m.Sets(g.MapStrAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})
	fmt.Println(m.Keys())

	// May Output:
	// [k1 k2 k3 k4]
}
Output:

func (*StrAnyMap) LockFunc

func (m *StrAnyMap) LockFunc(f func(m map[string]interface{}))

LockFunc locks writing with given callback function `f` within RWMutex.Lock.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.StrAnyMap
	m.Sets(g.MapStrAny{
		"k1": 1,
		"k2": 2,
		"k3": 3,
		"k4": 4,
	})

	m.LockFunc(func(m map[string]interface{}) {
		totalValue := 0
		for _, v := range m {
			totalValue += v.(int)
		}
		fmt.Println("totalValue:", totalValue)
	})

}
Output:

totalValue: 10

func (*StrAnyMap) Map

func (m *StrAnyMap) Map() map[string]interface{}

Map returns the underlying data map. Note that, if it's in concurrent-safe usage, it returns a copy of underlying data, or else a pointer to the underlying data.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	// non concurrent-safety, a pointer to the underlying data
	m1 := gmap.NewStrAnyMap()
	m1.Set("key1", "val1")
	fmt.Println("m1:", m1)

	n1 := m1.Map()
	fmt.Println("before n1:", n1)
	m1.Set("key1", "val2")
	fmt.Println("after n1:", n1)

	// concurrent-safety, copy of underlying data
	m2 := gmap.NewStrAnyMap(true)
	m2.Set("key1", "val1")
	fmt.Println("m2:", m2)

	n2 := m2.Map()
	fmt.Println("before n2:", n2)
	m2.Set("key1", "val2")
	fmt.Println("after n2:", n2)

}
Output:

m1: {"key1":"val1"}
before n1: map[key1:val1]
after n1: map[key1:val2]
m2: {"key1":"val1"}
before n2: map[key1:val1]
after n2: map[key1:val1]

func (*StrAnyMap) MapCopy

func (m *StrAnyMap) MapCopy() map[string]interface{}

MapCopy returns a copy of the underlying data of the hash map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrAnyMap()

	m.Set("key1", "val1")
	m.Set("key2", "val2")
	fmt.Println(m)

	n := m.MapCopy()
	fmt.Println(n)

}
Output:

{"key1":"val1","key2":"val2"}
map[key1:val1 key2:val2]

func (*StrAnyMap) MapStrAny

func (m *StrAnyMap) MapStrAny() map[string]interface{}

MapStrAny returns a copy of the underlying data of the map as map[string]interface{}.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrAnyMap()
	m.Set("key1", "val1")
	m.Set("key2", "val2")

	n := m.MapStrAny()
	fmt.Printf("%#v", n)

}
Output:

map[string]interface {}{"key1":"val1", "key2":"val2"}

func (StrAnyMap) MarshalJSON

func (m StrAnyMap) MarshalJSON() ([]byte, error)

MarshalJSON implements the interface MarshalJSON for json.Marshal.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/internal/json"
	"github.com/gogf/gf/v2/util/gconv"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.StrAnyMap
	m.Sets(g.MapStrAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})

	bytes, err := json.Marshal(&m)
	if err == nil {
		fmt.Println(gconv.String(bytes))
	}

}
Output:

{"k1":"v1","k2":"v2","k3":"v3","k4":"v4"}

func (*StrAnyMap) Merge

func (m *StrAnyMap) Merge(other *StrAnyMap)

Merge merges two hash maps. The `other` map will be merged into the map `m`.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m1, m2 gmap.StrAnyMap
	m1.Set("key1", "val1")
	m2.Set("key2", "val2")
	m1.Merge(&m2)
	fmt.Println(m1.Map())

	// May Output:
	// map[key1:val1 key2:val2]
}
Output:

func (*StrAnyMap) Pop

func (m *StrAnyMap) Pop() (key string, value interface{})

Pop retrieves and deletes an item from the map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.StrAnyMap
	m.Sets(g.MapStrAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})

	fmt.Println(m.Pop())

	// May Output:
	// k1 v1
}
Output:

func (*StrAnyMap) Pops

func (m *StrAnyMap) Pops(size int) map[string]interface{}

Pops retrieves and deletes `size` items from the map. It returns all items if size == -1.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.StrAnyMap
	m.Sets(g.MapStrAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})
	fmt.Println(m.Pops(-1))
	fmt.Println("size:", m.Size())

	m.Sets(g.MapStrAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})
	fmt.Println(m.Pops(2))
	fmt.Println("size:", m.Size())

	// May Output:
	// map[k1:v1 k2:v2 k3:v3 k4:v4]
	// size: 0
	// map[k1:v1 k2:v2]
	// size: 2
}
Output:

func (*StrAnyMap) RLockFunc

func (m *StrAnyMap) RLockFunc(f func(m map[string]interface{}))

RLockFunc locks reading with given callback function `f` within RWMutex.RLock.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.StrAnyMap
	m.Sets(g.MapStrAny{
		"k1": 1,
		"k2": 2,
		"k3": 3,
		"k4": 4,
	})

	m.RLockFunc(func(m map[string]interface{}) {
		totalValue := 0
		for _, v := range m {
			totalValue += v.(int)
		}
		fmt.Println("totalValue:", totalValue)
	})

}
Output:

totalValue: 10

func (*StrAnyMap) Remove

func (m *StrAnyMap) Remove(key string) (value interface{})

Remove deletes value from map by given `key`, and return this deleted value.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrAnyMap
	m.Set("k1", "v1")

	fmt.Println(m.Remove("k1"))
	fmt.Println(m.Remove("k2"))
	fmt.Println(m.Size())

}
Output:

v1
<nil>
0

func (*StrAnyMap) Removes

func (m *StrAnyMap) Removes(keys []string)

Removes batch deletes values of the map by keys.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.StrAnyMap
	m.Sets(g.MapStrAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})

	removeList := make([]string, 2)
	removeList = append(removeList, "k1")
	removeList = append(removeList, "k2")

	m.Removes(removeList)

	fmt.Println(m.Map())

}
Output:

map[k3:v3 k4:v4]

func (*StrAnyMap) Replace

func (m *StrAnyMap) Replace(data map[string]interface{})

Replace the data of the map with given `data`.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.StrAnyMap
	m.Sets(g.MapStrAny{
		"k1": "v1",
	})

	var n gmap.StrAnyMap
	n.Sets(g.MapStrAny{
		"k2": "v2",
	})

	fmt.Println(m.Map())

	m.Replace(n.Map())
	fmt.Println(m.Map())

	n.Set("k2", "v1")
	fmt.Println(m.Map())

}
Output:

map[k1:v1]
map[k2:v2]
map[k2:v1]

func (*StrAnyMap) Search

func (m *StrAnyMap) Search(key string) (value interface{}, found bool)

Search searches the map with given `key`. Second return parameter `found` is true if key was found, otherwise false.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrAnyMap()

	m.Set("key1", "val1")

	value, found := m.Search("key1")
	if found {
		fmt.Println("find key1 value:", value)
	}

	value, found = m.Search("key2")
	if !found {
		fmt.Println("key2 not find")
	}

}
Output:

find key1 value: val1
key2 not find

func (*StrAnyMap) Set

func (m *StrAnyMap) Set(key string, val interface{})

Set sets key-value to the hash map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrAnyMap()

	m.Set("key1", "val1")
	fmt.Println(m)

}
Output:

{"key1":"val1"}

func (*StrAnyMap) SetIfNotExist

func (m *StrAnyMap) SetIfNotExist(key string, value interface{}) bool

SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true. It returns false if `key` exists, and `value` would be ignored.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrAnyMap
	fmt.Println(m.SetIfNotExist("k1", "v1"))
	fmt.Println(m.SetIfNotExist("k1", "v2"))
	fmt.Println(m.Map())

}
Output:

true
false
map[k1:v1]

func (*StrAnyMap) SetIfNotExistFunc

func (m *StrAnyMap) SetIfNotExistFunc(key string, f func() interface{}) bool

SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and `value` would be ignored.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrAnyMap
	fmt.Println(m.SetIfNotExistFunc("k1", func() interface{} {
		return "v1"
	}))
	fmt.Println(m.SetIfNotExistFunc("k1", func() interface{} {
		return "v2"
	}))
	fmt.Println(m.Map())

}
Output:

true
false
map[k1:v1]

func (*StrAnyMap) SetIfNotExistFuncLock

func (m *StrAnyMap) SetIfNotExistFuncLock(key string, f func() interface{}) bool

SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and `value` would be ignored.

SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that it executes function `f` with mutex.Lock of the hash map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrAnyMap
	fmt.Println(m.SetIfNotExistFuncLock("k1", func() interface{} {
		return "v1"
	}))
	fmt.Println(m.SetIfNotExistFuncLock("k1", func() interface{} {
		return "v2"
	}))
	fmt.Println(m.Map())

}
Output:

true
false
map[k1:v1]

func (*StrAnyMap) Sets

func (m *StrAnyMap) Sets(data map[string]interface{})

Sets batch sets key-values to the hash map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrAnyMap()

	addMap := make(map[string]interface{})
	addMap["key1"] = "val1"
	addMap["key2"] = "val2"
	addMap["key3"] = "val3"

	m.Sets(addMap)
	fmt.Println(m)

}
Output:

{"key1":"val1","key2":"val2","key3":"val3"}

func (*StrAnyMap) Size

func (m *StrAnyMap) Size() int

Size returns the size of the map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.StrAnyMap
	m.Sets(g.MapStrAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})

	fmt.Println(m.Size())

}
Output:

4

func (*StrAnyMap) String

func (m *StrAnyMap) String() string

String returns the map as a string.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.StrAnyMap
	m.Sets(g.MapStrAny{
		"k1": "v1",
	})

	fmt.Println(m.String())

}
Output:

{"k1":"v1"}

func (*StrAnyMap) UnmarshalJSON

func (m *StrAnyMap) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/internal/json"
	"github.com/gogf/gf/v2/util/gconv"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.StrAnyMap
	m.Sets(g.MapStrAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})

	var n gmap.StrAnyMap

	err := json.Unmarshal(gconv.Bytes(m.String()), &n)
	if err == nil {
		fmt.Println(n.Map())
	}

}
Output:

map[k1:v1 k2:v2 k3:v3 k4:v4]

func (*StrAnyMap) UnmarshalValue

func (m *StrAnyMap) UnmarshalValue(value interface{}) (err error)

UnmarshalValue is an interface implement which sets any type of value for map.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/util/gconv"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrAnyMap

	goWeb := map[string]interface{}{
		"goframe": "https://goframe.org",
		"gin":     "https://gin-gonic.com/",
		"echo":    "https://echo.labstack.com/",
	}

	if err := gconv.Scan(goWeb, &m); err == nil {
		fmt.Printf("%#v", m.Map())
	}
}
Output:

map[string]interface {}{"echo":"https://echo.labstack.com/", "gin":"https://gin-gonic.com/", "goframe":"https://goframe.org"}

func (*StrAnyMap) Values

func (m *StrAnyMap) Values() []interface{}

Values returns all values of the map as a slice.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var m gmap.StrAnyMap
	m.Sets(g.MapStrAny{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})
	fmt.Println(m.Values())

	// May Output:
	// [v1 v2 v3 v4]
}
Output:

type StrIntMap

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

func NewStrIntMap

func NewStrIntMap(safe ...bool) *StrIntMap

NewStrIntMap returns an empty StrIntMap object. The parameter `safe` is used to specify whether using map in concurrent-safety, which is false in default.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrIntMap()

	m.Set("key1", 1)
	fmt.Println(m)

}
Output:

{"key1":1}

func NewStrIntMapFrom

func NewStrIntMapFrom(data map[string]int, safe ...bool) *StrIntMap

NewStrIntMapFrom creates and returns a hash map from given map `data`. Note that, the param `data` map will be set as the underlying data map(no deep copy), there might be some concurrent-safe issues when changing the map outside.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrIntMap()

	m.Set("key1", 1)
	fmt.Println(m)

	n := gmap.NewStrIntMapFrom(m.MapCopy(), true)
	fmt.Println(n)

}
Output:

{"key1":1}
{"key1":1}

func (*StrIntMap) Clear

func (m *StrIntMap) Clear()

Clear deletes all data of the map, it will remake a new underlying data map.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/frame/g"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrIntMap
	m.Sets(g.MapStrInt{
		"k1": 1,
		"k2": 2,
		"k3": 3,
		"k4": 4,
	})

	m.Clear()

	fmt.Println(m.Map())

}
Output:

map[]

func (*StrIntMap) Clone

func (m *StrIntMap) Clone() *StrIntMap

Clone returns a new hash map with copy of current map data.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrIntMap()

	m.Set("key1", 1)
	fmt.Println(m)

	n := m.Clone()
	fmt.Println(n)

}
Output:

{"key1":1}
{"key1":1}

func (*StrIntMap) Contains

func (m *StrIntMap) Contains(key string) bool

Contains checks whether a key exists. It returns true if the `key` exists, or else false.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/frame/g"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrIntMap
	m.Sets(g.MapStrInt{
		"k1": 1,
		"k2": 2,
		"k3": 3,
		"k4": 4,
	})

	fmt.Println(m.Contains("k1"))
	fmt.Println(m.Contains("k5"))

}
Output:

true
false

func (*StrIntMap) FilterEmpty

func (m *StrIntMap) FilterEmpty()

FilterEmpty deletes all key-value pair of which the value is empty. Values like: 0, nil, false, "", len(slice/map/chan) == 0 are considered empty.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/frame/g"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrIntMapFrom(g.MapStrInt{
		"k1": 0,
		"k2": 1,
	})
	m.FilterEmpty()
	fmt.Println(m.Map())

}
Output:

map[k2:1]

func (*StrIntMap) Flip

func (m *StrIntMap) Flip()

Flip exchanges key-value of the map to value-key.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/frame/g"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrIntMap
	m.Sets(g.MapStrInt{
		"k1": 1,
	})
	m.Flip()
	fmt.Println(m.Map())

	var n gmap.StrIntMap
	n.Sets(g.MapStrInt{
		"11": 1,
	})
	n.Flip()
	fmt.Println(n.Map())

}
Output:

map[1:0]
map[1:11]

func (*StrIntMap) Get

func (m *StrIntMap) Get(key string) (value int)

Get returns the value by given `key`.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrIntMap()

	m.Set("key1", 1)

	fmt.Println("key1 value:", m.Get("key1"))
	fmt.Println("key2 value:", m.Get("key2"))

}
Output:

key1 value: 1
key2 value: 0

func (*StrIntMap) GetOrSet

func (m *StrIntMap) GetOrSet(key string, value int) int

GetOrSet returns the value by key, or sets value with given `value` if it does not exist and then returns this value.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrIntMap()
	m.Set("key1", 1)

	fmt.Println(m.GetOrSet("key1", 0))
	fmt.Println(m.GetOrSet("key2", 2))

}
Output:

1
2

func (*StrIntMap) GetOrSetFunc

func (m *StrIntMap) GetOrSetFunc(key string, f func() int) int

GetOrSetFunc returns the value by key, or sets value with returned value of callback function `f` if it does not exist and then returns this value.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrIntMap()
	m.Set("key1", 1)

	fmt.Println(m.GetOrSetFunc("key1", func() int {
		return 0
	}))
	fmt.Println(m.GetOrSetFunc("key2", func() int {
		return 0
	}))

}
Output:

1
0

func (*StrIntMap) GetOrSetFuncLock

func (m *StrIntMap) GetOrSetFuncLock(key string, f func() int) int

GetOrSetFuncLock returns the value by key, or sets value with returned value of callback function `f` if it does not exist and then returns this value.

GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` with mutex.Lock of the hash map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrIntMap()
	m.Set("key1", 1)

	fmt.Println(m.GetOrSetFuncLock("key1", func() int {
		return 0
	}))
	fmt.Println(m.GetOrSetFuncLock("key2", func() int {
		return 0
	}))

}
Output:

1
0

func (*StrIntMap) IsEmpty

func (m *StrIntMap) IsEmpty() bool

IsEmpty checks whether the map is empty. It returns true if map is empty, or else false.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrIntMap
	fmt.Println(m.IsEmpty())

	m.Set("k1", 1)
	fmt.Println(m.IsEmpty())

}
Output:

true
false

func (*StrIntMap) Iterator

func (m *StrIntMap) Iterator(f func(k string, v int) bool)

Iterator iterates the hash map readonly with custom callback function `f`. If `f` returns true, then it continues iterating; or false to stop.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/util/gconv"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrIntMap()
	for i := 0; i < 10; i++ {
		m.Set(gconv.String(i), i*2)
	}

	var totalValue int
	m.Iterator(func(k string, v int) bool {
		totalValue += v

		return totalValue < 50
	})

	fmt.Println("totalValue:", totalValue)

	// May Output:
	// totalValue: 52
}
Output:

func (*StrIntMap) Keys

func (m *StrIntMap) Keys() []string

Keys returns all keys of the map as a slice.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/frame/g"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrIntMap
	m.Sets(g.MapStrInt{
		"k1": 1,
		"k2": 2,
		"k3": 3,
		"k4": 4,
	})
	fmt.Println(m.Keys())

	// May Output:
	// [k1 k2 k3 k4]
}
Output:

func (*StrIntMap) LockFunc

func (m *StrIntMap) LockFunc(f func(m map[string]int))

LockFunc locks writing with given callback function `f` within RWMutex.Lock.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/frame/g"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrIntMap
	m.Sets(g.MapStrInt{
		"k1": 1,
		"k2": 2,
		"k3": 3,
		"k4": 4,
	})

	m.LockFunc(func(m map[string]int) {
		totalValue := 0
		for _, v := range m {
			totalValue += v
		}
		fmt.Println("totalValue:", totalValue)
	})

}
Output:

totalValue: 10

func (*StrIntMap) Map

func (m *StrIntMap) Map() map[string]int

Map returns the underlying data map. Note that, if it's in concurrent-safe usage, it returns a copy of underlying data, or else a pointer to the underlying data.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	// non concurrent-safety, a pointer to the underlying data
	m1 := gmap.NewStrIntMap()
	m1.Set("key1", 1)
	fmt.Println("m1:", m1)

	n1 := m1.Map()
	fmt.Println("before n1:", n1)
	m1.Set("key1", 2)
	fmt.Println("after n1:", n1)

	// concurrent-safety, copy of underlying data
	m2 := gmap.NewStrIntMap(true)
	m2.Set("key1", 1)
	fmt.Println("m2:", m2)

	n2 := m2.Map()
	fmt.Println("before n2:", n2)
	m2.Set("key1", 2)
	fmt.Println("after n2:", n2)

}
Output:

m1: {"key1":1}
before n1: map[key1:1]
after n1: map[key1:2]
m2: {"key1":1}
before n2: map[key1:1]
after n2: map[key1:1]

func (*StrIntMap) MapCopy

func (m *StrIntMap) MapCopy() map[string]int

MapCopy returns a copy of the underlying data of the hash map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrIntMap()

	m.Set("key1", 1)
	m.Set("key2", 2)
	fmt.Println(m)

	n := m.MapCopy()
	fmt.Println(n)

}
Output:

{"key1":1,"key2":2}
map[key1:1 key2:2]

func (*StrIntMap) MapStrAny

func (m *StrIntMap) MapStrAny() map[string]interface{}

MapStrAny returns a copy of the underlying data of the map as map[string]interface{}.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrIntMap()
	m.Set("key1", 1)
	m.Set("key2", 2)

	n := m.MapStrAny()
	fmt.Printf("%#v", n)

}
Output:

map[string]interface {}{"key1":1, "key2":2}

func (StrIntMap) MarshalJSON

func (m StrIntMap) MarshalJSON() ([]byte, error)

MarshalJSON implements the interface MarshalJSON for json.Marshal.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/internal/json"
	"github.com/gogf/gf/v2/util/gconv"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrIntMap
	m.Sets(g.MapStrInt{
		"k1": 1,
		"k2": 2,
		"k3": 3,
		"k4": 4,
	})

	bytes, err := json.Marshal(&m)
	if err == nil {
		fmt.Println(gconv.String(bytes))
	}

}
Output:

{"k1":1,"k2":2,"k3":3,"k4":4}

func (*StrIntMap) Merge

func (m *StrIntMap) Merge(other *StrIntMap)

Merge merges two hash maps. The `other` map will be merged into the map `m`.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m1, m2 gmap.StrIntMap
	m1.Set("key1", 1)
	m2.Set("key2", 2)
	m1.Merge(&m2)
	fmt.Println(m1.Map())

	// May Output:
	// map[key1:1 key2:2]
}
Output:

func (*StrIntMap) Pop

func (m *StrIntMap) Pop() (key string, value int)

Pop retrieves and deletes an item from the map.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/frame/g"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrIntMap
	m.Sets(g.MapStrInt{
		"k1": 1,
		"k2": 2,
		"k3": 3,
		"k4": 4,
	})

	fmt.Println(m.Pop())

	// May Output:
	// k1 1
}
Output:

func (*StrIntMap) Pops

func (m *StrIntMap) Pops(size int) map[string]int

Pops retrieves and deletes `size` items from the map. It returns all items if size == -1.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/frame/g"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrIntMap
	m.Sets(g.MapStrInt{
		"k1": 1,
		"k2": 2,
		"k3": 3,
		"k4": 4,
	})
	fmt.Println(m.Pops(-1))
	fmt.Println("size:", m.Size())

	m.Sets(g.MapStrInt{
		"k1": 1,
		"k2": 2,
		"k3": 3,
		"k4": 4,
	})
	fmt.Println(m.Pops(2))
	fmt.Println("size:", m.Size())

	// May Output:
	// map[k1:1 k2:2 k3:3 k4:4]
	// size: 0
	// map[k1:1 k2:2]
	// size: 2
}
Output:

func (*StrIntMap) RLockFunc

func (m *StrIntMap) RLockFunc(f func(m map[string]int))

RLockFunc locks reading with given callback function `f` within RWMutex.RLock.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/frame/g"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrIntMap
	m.Sets(g.MapStrInt{
		"k1": 1,
		"k2": 2,
		"k3": 3,
		"k4": 4,
	})

	m.RLockFunc(func(m map[string]int) {
		totalValue := 0
		for _, v := range m {
			totalValue += v
		}
		fmt.Println("totalValue:", totalValue)
	})

}
Output:

totalValue: 10

func (*StrIntMap) Remove

func (m *StrIntMap) Remove(key string) (value int)

Remove deletes value from map by given `key`, and return this deleted value.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrIntMap
	m.Set("k1", 1)

	fmt.Println(m.Remove("k1"))
	fmt.Println(m.Remove("k2"))
	fmt.Println(m.Size())

}
Output:

1
0
0

func (*StrIntMap) Removes

func (m *StrIntMap) Removes(keys []string)

Removes batch deletes values of the map by keys.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/frame/g"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrIntMap
	m.Sets(g.MapStrInt{
		"k1": 1,
		"k2": 2,
		"k3": 3,
		"k4": 4,
	})

	removeList := make([]string, 2)
	removeList = append(removeList, "k1")
	removeList = append(removeList, "k2")

	m.Removes(removeList)

	fmt.Println(m.Map())

}
Output:

map[k3:3 k4:4]

func (*StrIntMap) Replace

func (m *StrIntMap) Replace(data map[string]int)

Replace the data of the map with given `data`.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/frame/g"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrIntMap
	m.Sets(g.MapStrInt{
		"k1": 1,
	})

	var n gmap.StrIntMap
	n.Sets(g.MapStrInt{
		"k2": 2,
	})

	fmt.Println(m.Map())

	m.Replace(n.Map())
	fmt.Println(m.Map())

	n.Set("k2", 1)
	fmt.Println(m.Map())

}
Output:

map[k1:1]
map[k2:2]
map[k2:1]

func (*StrIntMap) Search

func (m *StrIntMap) Search(key string) (value int, found bool)

Search searches the map with given `key`. Second return parameter `found` is true if key was found, otherwise false.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrIntMap()

	m.Set("key1", 1)

	value, found := m.Search("key1")
	if found {
		fmt.Println("find key1 value:", value)
	}

	value, found = m.Search("key2")
	if !found {
		fmt.Println("key2 not find")
	}

}
Output:

find key1 value: 1
key2 not find

func (*StrIntMap) Set

func (m *StrIntMap) Set(key string, val int)

Set sets key-value to the hash map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrIntMap()

	m.Set("key1", 1)
	fmt.Println(m)

}
Output:

{"key1":1}

func (*StrIntMap) SetIfNotExist

func (m *StrIntMap) SetIfNotExist(key string, value int) bool

SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true. It returns false if `key` exists, and `value` would be ignored.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrIntMap
	fmt.Println(m.SetIfNotExist("k1", 1))
	fmt.Println(m.SetIfNotExist("k1", 2))
	fmt.Println(m.Map())

}
Output:

true
false
map[k1:1]

func (*StrIntMap) SetIfNotExistFunc

func (m *StrIntMap) SetIfNotExistFunc(key string, f func() int) bool

SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and `value` would be ignored.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrIntMap
	fmt.Println(m.SetIfNotExistFunc("k1", func() int {
		return 1
	}))
	fmt.Println(m.SetIfNotExistFunc("k1", func() int {
		return 2
	}))
	fmt.Println(m.Map())

}
Output:

true
false
map[k1:1]

func (*StrIntMap) SetIfNotExistFuncLock

func (m *StrIntMap) SetIfNotExistFuncLock(key string, f func() int) bool

SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and `value` would be ignored.

SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that it executes function `f` with mutex.Lock of the hash map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrIntMap
	fmt.Println(m.SetIfNotExistFuncLock("k1", func() int {
		return 1
	}))
	fmt.Println(m.SetIfNotExistFuncLock("k1", func() int {
		return 2
	}))
	fmt.Println(m.Map())

}
Output:

true
false
map[k1:1]

func (*StrIntMap) Sets

func (m *StrIntMap) Sets(data map[string]int)

Sets batch sets key-values to the hash map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrIntMap()

	addMap := make(map[string]int)
	addMap["key1"] = 1
	addMap["key2"] = 2
	addMap["key3"] = 3

	m.Sets(addMap)
	fmt.Println(m)

}
Output:

{"key1":1,"key2":2,"key3":3}

func (*StrIntMap) Size

func (m *StrIntMap) Size() int

Size returns the size of the map.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/frame/g"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrIntMap
	m.Sets(g.MapStrInt{
		"k1": 1,
		"k2": 2,
		"k3": 3,
		"k4": 4,
	})

	fmt.Println(m.Size())

}
Output:

4

func (*StrIntMap) String

func (m *StrIntMap) String() string

String returns the map as a string.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/frame/g"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrIntMap
	m.Sets(g.MapStrInt{
		"k1": 1,
	})

	fmt.Println(m.String())

}
Output:

{"k1":1}

func (*StrIntMap) UnmarshalJSON

func (m *StrIntMap) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/internal/json"
	"github.com/gogf/gf/v2/util/gconv"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrIntMap
	m.Sets(g.MapStrInt{
		"k1": 1,
		"k2": 2,
		"k3": 3,
		"k4": 4,
	})

	var n gmap.StrIntMap

	err := json.Unmarshal(gconv.Bytes(m.String()), &n)
	if err == nil {
		fmt.Println(n.Map())
	}

}
Output:

map[k1:1 k2:2 k3:3 k4:4]

func (*StrIntMap) UnmarshalValue

func (m *StrIntMap) UnmarshalValue(value interface{}) (err error)

UnmarshalValue is an interface implement which sets any type of value for map.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/util/gconv"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrIntMap

	goWeb := map[string]int{
		"goframe": 1,
		"gin":     2,
		"echo":    3,
	}

	if err := gconv.Scan(goWeb, &m); err == nil {
		fmt.Printf("%#v", m.Map())
	}
}
Output:

map[string]int{"echo":3, "gin":2, "goframe":1}

func (*StrIntMap) Values

func (m *StrIntMap) Values() []int

Values returns all values of the map as a slice.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/frame/g"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrIntMap
	m.Sets(g.MapStrInt{
		"k1": 1,
		"k2": 2,
		"k3": 3,
		"k4": 4,
	})
	fmt.Println(m.Values())

	// May Output:
	// [1 2 3 4]
}
Output:

type StrStrMap

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

func NewStrStrMap

func NewStrStrMap(safe ...bool) *StrStrMap

NewStrStrMap returns an empty StrStrMap object. The parameter `safe` is used to specify whether using map in concurrent-safety, which is false in default.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrStrMap()

	m.Set("key1", "var1")
	fmt.Println(m)

}
Output:

{"key1":"var1"}

func NewStrStrMapFrom

func NewStrStrMapFrom(data map[string]string, safe ...bool) *StrStrMap

NewStrStrMapFrom creates and returns a hash map from given map `data`. Note that, the param `data` map will be set as the underlying data map(no deep copy), there might be some concurrent-safe issues when changing the map outside.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrStrMap()

	m.Set("key1", "var1")
	fmt.Println(m)

	n := gmap.NewStrStrMapFrom(m.MapCopy(), true)
	fmt.Println(n)

}
Output:

{"key1":"var1"}
{"key1":"var1"}

func (*StrStrMap) Clear

func (m *StrStrMap) Clear()

Clear deletes all data of the map, it will remake a new underlying data map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/frame/g"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrStrMap
	m.Sets(g.MapStrStr{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})

	m.Clear()

	fmt.Println(m.Map())

}
Output:

map[]

func (*StrStrMap) Clone

func (m *StrStrMap) Clone() *StrStrMap

Clone returns a new hash map with copy of current map data.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrStrMap()

	m.Set("key1", "val1")
	fmt.Println(m)

	n := m.Clone()
	fmt.Println(n)

}
Output:

{"key1":"val1"}
{"key1":"val1"}

func (*StrStrMap) Contains

func (m *StrStrMap) Contains(key string) bool

Contains checks whether a key exists. It returns true if the `key` exists, or else false.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/frame/g"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrStrMap
	m.Sets(g.MapStrStr{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})

	fmt.Println(m.Contains("k1"))
	fmt.Println(m.Contains("k5"))

}
Output:

true
false

func (*StrStrMap) FilterEmpty

func (m *StrStrMap) FilterEmpty()

FilterEmpty deletes all key-value pair of which the value is empty. Values like: 0, nil, false, "", len(slice/map/chan) == 0 are considered empty.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/frame/g"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrStrMapFrom(g.MapStrStr{
		"k1": "",
		"k2": "v2",
	})
	m.FilterEmpty()
	fmt.Println(m.Map())

}
Output:

map[k2:v2]

func (*StrStrMap) Flip

func (m *StrStrMap) Flip()

Flip exchanges key-value of the map to value-key.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/frame/g"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrStrMap
	m.Sets(g.MapStrStr{
		"k1": "v1",
	})
	m.Flip()
	fmt.Println(m.Map())

}
Output:

map[v1:k1]

func (*StrStrMap) Get

func (m *StrStrMap) Get(key string) (value string)

Get returns the value by given `key`.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrStrMap()

	m.Set("key1", "val1")

	fmt.Println("key1 value:", m.Get("key1"))
	fmt.Println("key2 value:", m.Get("key2"))

}
Output:

key1 value: val1
key2 value:

func (*StrStrMap) GetOrSet

func (m *StrStrMap) GetOrSet(key string, value string) string

GetOrSet returns the value by key, or sets value with given `value` if it does not exist and then returns this value.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrStrMap()
	m.Set("key1", "val1")

	fmt.Println(m.GetOrSet("key1", "NotExistValue"))
	fmt.Println(m.GetOrSet("key2", "val2"))

}
Output:

val1
val2

func (*StrStrMap) GetOrSetFunc

func (m *StrStrMap) GetOrSetFunc(key string, f func() string) string

GetOrSetFunc returns the value by key, or sets value with returned value of callback function `f` if it does not exist and then returns this value.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrStrMap()
	m.Set("key1", "val1")

	fmt.Println(m.GetOrSetFunc("key1", func() string {
		return "NotExistValue"
	}))
	fmt.Println(m.GetOrSetFunc("key2", func() string {
		return "NotExistValue"
	}))

}
Output:

val1
NotExistValue

func (*StrStrMap) GetOrSetFuncLock

func (m *StrStrMap) GetOrSetFuncLock(key string, f func() string) string

GetOrSetFuncLock returns the value by key, or sets value with returned value of callback function `f` if it does not exist and then returns this value.

GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f` with mutex.Lock of the hash map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrStrMap()
	m.Set("key1", "val1")

	fmt.Println(m.GetOrSetFuncLock("key1", func() string {
		return "NotExistValue"
	}))
	fmt.Println(m.GetOrSetFuncLock("key2", func() string {
		return "NotExistValue"
	}))

}
Output:

val1
NotExistValue

func (*StrStrMap) IsEmpty

func (m *StrStrMap) IsEmpty() bool

IsEmpty checks whether the map is empty. It returns true if map is empty, or else false.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrStrMap
	fmt.Println(m.IsEmpty())

	m.Set("k1", "v1")
	fmt.Println(m.IsEmpty())

}
Output:

true
false

func (*StrStrMap) Iterator

func (m *StrStrMap) Iterator(f func(k string, v string) bool)

Iterator iterates the hash map readonly with custom callback function `f`. If `f` returns true, then it continues iterating; or false to stop.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/util/gconv"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrStrMap()
	for i := 0; i < 10; i++ {
		m.Set("key"+gconv.String(i), "var"+gconv.String(i))
	}

	var str string
	m.Iterator(func(k string, v string) bool {

		str += v + "|"

		return len(str) < 20
	})

	fmt.Println("str:", str)

	// May Output:
	// var0|var1|var2|var3|
}
Output:

func (*StrStrMap) Keys

func (m *StrStrMap) Keys() []string

Keys returns all keys of the map as a slice.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/frame/g"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrStrMap
	m.Sets(g.MapStrStr{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})
	fmt.Println(m.Keys())

	// May Output:
	// [k1 k2 k3 k4]
}
Output:

func (*StrStrMap) LockFunc

func (m *StrStrMap) LockFunc(f func(m map[string]string))

LockFunc locks writing with given callback function `f` within RWMutex.Lock.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/frame/g"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrStrMap
	m.Sets(g.MapStrStr{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})

	m.LockFunc(func(m map[string]string) {
		for k, v := range m {
			fmt.Println("key:", k, " value:", v)
		}
	})

	// May Output:
	// key: k1  value: v1
	// key: k2  value: v2
	// key: k3  value: v3
	// key: k4  value: v4
}
Output:

func (*StrStrMap) Map

func (m *StrStrMap) Map() map[string]string

Map returns the underlying data map. Note that, if it's in concurrent-safe usage, it returns a copy of underlying data, or else a pointer to the underlying data.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	// non concurrent-safety, a pointer to the underlying data
	m1 := gmap.NewStrStrMap()
	m1.Set("key1", "val1")
	fmt.Println("m1:", m1)

	n1 := m1.Map()
	fmt.Println("before n1:", n1)
	m1.Set("key1", "val2")
	fmt.Println("after n1:", n1)

	// concurrent-safety, copy of underlying data
	m2 := gmap.NewStrStrMap(true)
	m2.Set("key1", "val1")
	fmt.Println("m2:", m2)

	n2 := m2.Map()
	fmt.Println("before n2:", n2)
	m2.Set("key1", "val2")
	fmt.Println("after n2:", n2)

}
Output:

m1: {"key1":"val1"}
before n1: map[key1:val1]
after n1: map[key1:val2]
m2: {"key1":"val1"}
before n2: map[key1:val1]
after n2: map[key1:val1]

func (*StrStrMap) MapCopy

func (m *StrStrMap) MapCopy() map[string]string

MapCopy returns a copy of the underlying data of the hash map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrStrMap()

	m.Set("key1", "val1")
	m.Set("key2", "val2")
	fmt.Println(m)

	n := m.MapCopy()
	fmt.Println(n)

}
Output:

{"key1":"val1","key2":"val2"}
map[key1:val1 key2:val2]

func (*StrStrMap) MapStrAny

func (m *StrStrMap) MapStrAny() map[string]interface{}

MapStrAny returns a copy of the underlying data of the map as map[string]interface{}.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrStrMap()
	m.Set("key1", "val1")
	m.Set("key2", "val2")

	n := m.MapStrAny()
	fmt.Printf("%#v", n)

}
Output:

map[string]interface {}{"key1":"val1", "key2":"val2"}

func (StrStrMap) MarshalJSON

func (m StrStrMap) MarshalJSON() ([]byte, error)

MarshalJSON implements the interface MarshalJSON for json.Marshal.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/internal/json"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrStrMap
	m.Sets(g.MapStrStr{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})

	bytes, err := json.Marshal(&m)
	if err == nil {
		fmt.Println(gconv.String(bytes))
	}

}
Output:

{"k1":"v1","k2":"v2","k3":"v3","k4":"v4"}

func (*StrStrMap) Merge

func (m *StrStrMap) Merge(other *StrStrMap)

Merge merges two hash maps. The `other` map will be merged into the map `m`.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m1, m2 gmap.StrStrMap
	m1.Set("key1", "val1")
	m2.Set("key2", "val2")
	m1.Merge(&m2)
	fmt.Println(m1.Map())

	// May Output:
	// map[key1:val1 key2:val2]
}
Output:

func (*StrStrMap) Pop

func (m *StrStrMap) Pop() (key, value string)

Pop retrieves and deletes an item from the map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/frame/g"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrStrMap
	m.Sets(g.MapStrStr{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})

	fmt.Println(m.Pop())

	// May Output:
	// k1 v1
}
Output:

func (*StrStrMap) Pops

func (m *StrStrMap) Pops(size int) map[string]string

Pops retrieves and deletes `size` items from the map. It returns all items if size == -1.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/frame/g"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrStrMap
	m.Sets(g.MapStrStr{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})
	fmt.Println(m.Pops(-1))
	fmt.Println("size:", m.Size())

	m.Sets(g.MapStrStr{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})
	fmt.Println(m.Pops(2))
	fmt.Println("size:", m.Size())

	// May Output:
	// map[k1:v1 k2:v2 k3:v3 k4:v4]
	// size: 0
	// map[k1:v1 k2:v2]
	// size: 2
}
Output:

func (*StrStrMap) RLockFunc

func (m *StrStrMap) RLockFunc(f func(m map[string]string))

RLockFunc locks reading with given callback function `f` within RWMutex.RLock.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/frame/g"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrStrMap
	m.Sets(g.MapStrStr{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})

	m.RLockFunc(func(m map[string]string) {
		for k, v := range m {
			fmt.Println("key:", k, " value:", v)
		}
	})

	// May Output:
	// key: k1  value: v1
	// key: k2  value: v2
	// key: k3  value: v3
	// key: k4  value: v4
}
Output:

func (*StrStrMap) Remove

func (m *StrStrMap) Remove(key string) (value string)

Remove deletes value from map by given `key`, and return this deleted value.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrStrMap
	m.Set("k1", "v1")

	fmt.Println(m.Remove("k1"))
	fmt.Println(len(m.Remove("k2")))
	fmt.Println(m.Size())

}
Output:

v1
0
0

func (*StrStrMap) Removes

func (m *StrStrMap) Removes(keys []string)

Removes batch deletes values of the map by keys.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/frame/g"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrStrMap
	m.Sets(g.MapStrStr{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})

	removeList := make([]string, 2)
	removeList = append(removeList, "k1")
	removeList = append(removeList, "k2")

	m.Removes(removeList)

	fmt.Println(m.Map())

}
Output:

map[k3:v3 k4:v4]

func (*StrStrMap) Replace

func (m *StrStrMap) Replace(data map[string]string)

Replace the data of the map with given `data`.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/frame/g"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrStrMap
	m.Sets(g.MapStrStr{
		"k1": "v1",
	})

	var n gmap.StrStrMap
	n.Sets(g.MapStrStr{
		"k2": "v2",
	})

	fmt.Println(m.Map())

	m.Replace(n.Map())
	fmt.Println(m.Map())

	n.Set("k2", "v1")
	fmt.Println(m.Map())

}
Output:

map[k1:v1]
map[k2:v2]
map[k2:v1]

func (*StrStrMap) Search

func (m *StrStrMap) Search(key string) (value string, found bool)

Search searches the map with given `key`. Second return parameter `found` is true if key was found, otherwise false.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrStrMap()

	m.Set("key1", "val1")

	value, found := m.Search("key1")
	if found {
		fmt.Println("find key1 value:", value)
	}

	value, found = m.Search("key2")
	if !found {
		fmt.Println("key2 not find")
	}

}
Output:

find key1 value: val1
key2 not find

func (*StrStrMap) Set

func (m *StrStrMap) Set(key string, val string)

Set sets key-value to the hash map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrStrMap()

	m.Set("key1", "val1")
	fmt.Println(m)

}
Output:

{"key1":"val1"}

func (*StrStrMap) SetIfNotExist

func (m *StrStrMap) SetIfNotExist(key string, value string) bool

SetIfNotExist sets `value` to the map if the `key` does not exist, and then returns true. It returns false if `key` exists, and `value` would be ignored.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrStrMap
	fmt.Println(m.SetIfNotExist("k1", "v1"))
	fmt.Println(m.SetIfNotExist("k1", "v2"))
	fmt.Println(m.Map())

}
Output:

true
false
map[k1:v1]

func (*StrStrMap) SetIfNotExistFunc

func (m *StrStrMap) SetIfNotExistFunc(key string, f func() string) bool

SetIfNotExistFunc sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and `value` would be ignored.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrStrMap
	fmt.Println(m.SetIfNotExistFunc("k1", func() string {
		return "v1"
	}))
	fmt.Println(m.SetIfNotExistFunc("k1", func() string {
		return "v2"
	}))
	fmt.Println(m.Map())

}
Output:

true
false
map[k1:v1]

func (*StrStrMap) SetIfNotExistFuncLock

func (m *StrStrMap) SetIfNotExistFuncLock(key string, f func() string) bool

SetIfNotExistFuncLock sets value with return value of callback function `f`, and then returns true. It returns false if `key` exists, and `value` would be ignored.

SetIfNotExistFuncLock differs with SetIfNotExistFunc function is that it executes function `f` with mutex.Lock of the hash map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrStrMap
	fmt.Println(m.SetIfNotExistFuncLock("k1", func() string {
		return "v1"
	}))
	fmt.Println(m.SetIfNotExistFuncLock("k1", func() string {
		return "v2"
	}))
	fmt.Println(m.Map())

}
Output:

true
false
map[k1:v1]

func (*StrStrMap) Sets

func (m *StrStrMap) Sets(data map[string]string)

Sets batch sets key-values to the hash map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewStrStrMap()

	addMap := make(map[string]string)
	addMap["key1"] = "val1"
	addMap["key2"] = "val2"
	addMap["key3"] = "val3"

	m.Sets(addMap)
	fmt.Println(m)

}
Output:

{"key1":"val1","key2":"val2","key3":"val3"}

func (*StrStrMap) Size

func (m *StrStrMap) Size() int

Size returns the size of the map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/frame/g"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrStrMap
	m.Sets(g.MapStrStr{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})

	fmt.Println(m.Size())

}
Output:

4

func (*StrStrMap) String

func (m *StrStrMap) String() string

String returns the map as a string.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/frame/g"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrStrMap
	m.Sets(g.MapStrStr{
		"k1": "v1",
	})

	fmt.Println(m.String())

}
Output:

{"k1":"v1"}

func (*StrStrMap) UnmarshalJSON

func (m *StrStrMap) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.

Example
package main

import (
	"fmt"
	"github.com/gogf/gf/v2/internal/json"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrStrMap
	m.Sets(g.MapStrStr{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})

	var n gmap.StrStrMap

	err := json.Unmarshal(gconv.Bytes(m.String()), &n)
	if err == nil {
		fmt.Println(n.Map())
	}

}
Output:

map[k1:v1 k2:v2 k3:v3 k4:v4]

func (*StrStrMap) UnmarshalValue

func (m *StrStrMap) UnmarshalValue(value interface{}) (err error)

UnmarshalValue is an interface implement which sets any type of value for map.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/util/gconv"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrStrMap

	goWeb := map[string]string{
		"goframe": "https://goframe.org",
		"gin":     "https://gin-gonic.com/",
		"echo":    "https://echo.labstack.com/",
	}

	if err := gconv.Scan(goWeb, &m); err == nil {
		fmt.Printf("%#v", m.Map())
	}
}
Output:

map[string]string{"echo":"https://echo.labstack.com/", "gin":"https://gin-gonic.com/", "goframe":"https://goframe.org"}

func (*StrStrMap) Values

func (m *StrStrMap) Values() []string

Values returns all values of the map as a slice.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/frame/g"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	var m gmap.StrStrMap
	m.Sets(g.MapStrStr{
		"k1": "v1",
		"k2": "v2",
		"k3": "v3",
		"k4": "v4",
	})
	fmt.Println(m.Values())

	// May Output:
	// [v1 v2 v3 v4]
}
Output:

type TreeMap

type TreeMap = gtree.RedBlackTree

TreeMap based on red-black tree, alias of RedBlackTree.

func NewTreeMap

func NewTreeMap(comparator func(v1, v2 interface{}) int, safe ...bool) *TreeMap

NewTreeMap instantiates a tree map with the custom comparator. The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/util/gutil"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewTreeMap(gutil.ComparatorString)

	m.Set("key2", "var2")
	m.Set("key1", "var1")

	fmt.Println(m.Map())

	// May Output:
	// map[key1:var1 key2:var2]
}
Output:

func NewTreeMapFrom

func NewTreeMapFrom(comparator func(v1, v2 interface{}) int, data map[interface{}]interface{}, safe ...bool) *TreeMap

NewTreeMapFrom instantiates a tree map with the custom comparator and `data` map. Note that, the param `data` map will be set as the underlying data map(no deep copy), there might be some concurrent-safe issues when changing the map outside. The parameter `safe` is used to specify whether using tree in concurrent-safety, which is false in default.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/util/gutil"

	"github.com/gogf/gf/v2/container/gmap"
)

func main() {
	m := gmap.NewTreeMap(gutil.ComparatorString)

	m.Set("key2", "var2")
	m.Set("key1", "var1")

	fmt.Println(m.Map())

	n := gmap.NewListMapFrom(m.Map(), true)
	fmt.Println(n.Map())

	// May Output:
	// map[key1:var1 key2:var2]
	// map[key1:var1 key2:var2]
}
Output:

Jump to

Keyboard shortcuts

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