Documentation
¶
Overview ¶
Package dict provides a flexible hash map implementation.
Index ¶
- type Dict
- func (d *Dict[K, V]) Clear() *Dict[K, V]
- func (d Dict[K, V]) Copy() Dict[K, V]
- func (d *Dict[K, V]) Delete(key K) bool
- func (d Dict[K, V]) Empty() bool
- func (d Dict[K, V]) Equal(another Dict[K, V]) bool
- func (d Dict[K, V]) Get(key K, defaultValue ...V) (value V)
- func (d Dict[K, V]) Has(key K) bool
- func (d Dict[K, V]) Items() []*DictItem[K, V]
- func (d Dict[K, V]) Keys() []K
- func (d Dict[K, V]) MarshalJSON() ([]byte, error)
- func (d *Dict[K, V]) Pop(key K, args ...V) (value V, err error)
- func (d *Dict[K, V]) PopItem() (key K, value V, err error)
- func (d *Dict[K, V]) Set(key K, value V) *Dict[K, V]
- func (d Dict[K, V]) Size() int
- func (d *Dict[K, V]) UnmarshalJSON(data []byte) (err error)
- func (d *Dict[K, V]) Update(another Dict[K, V]) *Dict[K, V]
- func (d Dict[K, V]) Values() []V
- type DictItem
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Dict ¶
type Dict[K comparable, V any] map[K]V
Dict is a generic type representing a dictionary with keys of type K and values of type V.
Example ¶
d := Dict[string, int]{"a": 1, "b": 2} fmt.Println(d)
Output: map[a:1 b:2]
func (Dict[K, V]) Copy ¶
Copy creates a shallow copy of the dictionary.
Example ¶
d := Dict[string, int]{"a": 1, "b": 2} backup := d.Copy() fmt.Println(d) fmt.Println(backup)
Output: map[a:1 b:2] map[a:1 b:2]
func (*Dict[K, V]) Delete ¶
Delete removes the key-value pair with the specified key from the dictionary. It returns true if the key exists in the dictionary and false otherwise.
Example ¶
d := Dict[string, int]{"a": 1} fmt.Println(d.Has("a")) fmt.Println(d.Delete("a")) fmt.Println(d.Has("a"))
Output: true true false
func (Dict[K, V]) Empty ¶
Empty checks if the dictionary is empty.
Example ¶
d := Dict[string, string]{} fmt.Println(d.Empty()) d.Set("key", "value") fmt.Println(d.Empty())
Output: true false
func (Dict[K, V]) Equal ¶
Equal compares two dictionaries for equality based on their keys and values.
Example ¶
d1 := Dict[string, string]{"key1": "value1", "key2": "value2"} d2 := Dict[string, string]{"key1": "value1", "key2": "value2"} d3 := Dict[string, string]{"key1": "value1", "key2": "value3"} d4 := Dict[string, string]{"key1": "value1"} fmt.Println(d1.Equal(d2)) fmt.Println(d1.Equal(d3)) fmt.Println(d1.Equal(d4))
Output: true false false
func (Dict[K, V]) Get ¶
func (d Dict[K, V]) Get(key K, defaultValue ...V) (value V)
Get retrieves the value associated with the specified key, and returns a default value if the key is not present.
Example ¶
d := Dict[string, int]{"one": 1, "two": 2, "three": 3} fmt.Println(d.Get("two")) fmt.Println(d.Get("four")) fmt.Println(d.Get("four", -1))
Output: 2 0 -1
func (Dict[K, V]) Has ¶
Has checks if the dictionary contains the specified key.
Example ¶
d := Dict[string, string]{"key1": "value1", "key2": "value2"} fmt.Println(d.Has("key1")) fmt.Println(d.Has("key3"))
Output: true false
func (Dict[K, V]) Items ¶
Items returns a slice of pointers to DictItem, each representing a key-value pair in the dictionary.
func (Dict[K, V]) Keys ¶
func (d Dict[K, V]) Keys() []K
Keys returns a slice containing all the keys in the dictionary.
func (Dict[K, V]) MarshalJSON ¶
MarshalJSON converts the dictionary to JSON format.
Example ¶
dict := Dict[string, any]{"name": "John", "age": 30} jsonBytes, _ := json.Marshal(dict) fmt.Println(string(jsonBytes))
Output: {"age":30,"name":"John"}
func (*Dict[K, V]) Pop ¶
Pop removes the key and returns its value. If the key exists, its value is returned. If it doesn't exist and a default value is provided, the default value is returned. If the key doesn't exist and no default value is provided, an error is returned.
Example ¶
d := Dict[string, int]{"a": 1, "b": 2, "c": 3} fmt.Println(d.Has("a")) fmt.Println(d.Pop("a")) fmt.Println(d.Has("a")) fmt.Println(d.Pop("a")) fmt.Println(d.Pop("a", -1))
Output: true 1 <nil> false 0 the key is not found in the dict -1 <nil>
func (*Dict[K, V]) PopItem ¶
PopItem removes and returns an arbitrary key-value pair from the dictionary.
func (*Dict[K, V]) Set ¶
Set adds or updates the key with the specified value in the dictionary.
Example ¶
d := make(Dict[string, int]) fmt.Println(d.Get("a", 0)) fmt.Println(d.Has("a")) d.Set("a", 5) fmt.Println(d.Has("a")) fmt.Println(d.Get("a", 0)) d.Set("a", 10) fmt.Println(d.Get("a", 0))
Output: 0 false true 5 10
func (Dict[K, V]) Size ¶
Size returns the number of key-value pairs in the dictionary.
Example ¶
d := Dict[string, int]{"one": 1, "two": 2, "three": 3} fmt.Println(d.Size())
Output: 3
func (*Dict[K, V]) UnmarshalJSON ¶
UnmarshalJSON parses the JSON data into the dictionary.
Example ¶
jsonStr := []byte(`{"age":30,"name":"John"}`) var dict Dict[string, any] _ = json.Unmarshal(jsonStr, &dict) fmt.Println(dict)
Output: map[age:30 name:John]
func (*Dict[K, V]) Update ¶
Update merges the key-value pairs from another dictionary into the current dictionary.
Example ¶
d1 := Dict[string, int]{"a": 1, "b": 2} d2 := Dict[string, int]{"b": 3, "c": 4} fmt.Println(d1) fmt.Println(d2) d1.Update(d2) fmt.Println(d1) fmt.Println(d2)
Output: map[a:1 b:2] map[b:3 c:4] map[a:1 b:3 c:4] map[b:3 c:4]
type DictItem ¶
type DictItem[K comparable, V any] struct { Key K Value V }
DictItem represents a key-value pair in the dictionary.