Documentation
¶
Overview ¶
Package dict provides a flexible hash map implementation.
Index ¶
- type Dict
- func (d *Dict) Clear() *Dict
- func (d Dict) Copy() Dict
- func (d *Dict) Delete(key any) bool
- func (d Dict) Empty() bool
- func (d Dict) Equal(another Dict) bool
- func (d Dict) Get(key any, defaultValue ...any) (value any)
- func (d Dict) Has(key any) bool
- func (d Dict) Items() [][2]any
- func (d Dict) Keys() []any
- func (d Dict) MarshalJSON() ([]byte, error)
- func (d *Dict) Pop(key any, args ...any) (value any, err error)
- func (d *Dict) PopItem() (key, value any, err error)
- func (d *Dict) Set(key, value any) *Dict
- func (d Dict) Size() int
- func (d *Dict) UnmarshalJSON(data []byte) (err error)
- func (d *Dict) Update(another Dict) *Dict
- func (d Dict) Values() []any
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Dict ¶
Dict is an enhanced map[any]any that provides additional methods for working with dictionaries.
Example ¶
m := map[any]any{"a": 1, "b": 2}
d := Dict(m)
fmt.Println(d)
Output: map[a:1 b:2]
func FromEntries ¶
FromEntries method creates a new dictionary from the provided key-value pairs.
Example ¶
d := FromEntries([2]any{"a", 1}, [2]any{"b", 2})
fmt.Println(d)
Output: map[a:1 b:2]
func (*Dict) Clear ¶
Clear method removes all key-value pairs from the dictionary and returns the empty dictionary.
Example ¶
d := Dict{"a": 1}
fmt.Println(d)
d.Clear()
fmt.Println(d)
fmt.Println(d.Size())
fmt.Println(d.Empty())
Output: map[a:1] map[] 0 true
func (Dict) Copy ¶
Copy method creates a shallow copy of the dictionary.
Example ¶
d := Dict{"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) Delete ¶
Delete method removes the key-value pair for the given key from the dictionary and returns true if the key existed, false otherwise.
Example ¶
d := Dict{"a": 1}
fmt.Println(d.Has("a"))
fmt.Println(d.Delete("a"))
fmt.Println(d.Has("a"))
Output: true true false
func (Dict) Empty ¶
Empty returns true if the dictionary is empty, otherwise returns false.
Example ¶
d := Dict{}
fmt.Println(d.Empty())
d.Set("key", "value")
fmt.Println(d.Empty())
Output: true false
func (Dict) Equal ¶
Equal checks if the current dictionary is equal to the provided dictionary and returns true if they are equal, otherwise returns false.
Example ¶
d1 := Dict{"key1": "value1", "key2": "value2"}
d2 := Dict{"key1": "value1", "key2": "value2"}
d3 := Dict{"key1": "value1", "key2": "value3"}
d4 := Dict{"key1": "value1"}
fmt.Println(d1.Equal(d2))
fmt.Println(d1.Equal(d3))
fmt.Println(d1.Equal(d4))
Output: true false false
func (Dict) Get ¶
Get retrieves the value associated with the specified key. If the key is not found, it returns the default value.
Example ¶
d := Dict{"one": 1, "two": 2, "three": 3}
fmt.Println(d.Get("two"))
fmt.Println(d.Get("four"))
fmt.Println(d.Get("four", "not found"))
Output: 2 <nil> not found
func (Dict) Has ¶
Has checks if the key exists in the dictionary and returns true if found, otherwise returns false.
Example ¶
d := Dict{"key1": "value1", "key2": "value2"}
fmt.Println(d.Has("key1"))
fmt.Println(d.Has("key3"))
Output: true false
func (Dict) Items ¶
Items returns a slice containing all the key-value pairs present in the dictionary.
func (Dict) MarshalJSON ¶
MarshalJSON converts the dictionary to JSON format.
Example ¶
dict := Dict{"name": "John", "age": 30}
jsonBytes, _ := json.Marshal(dict)
fmt.Println(string(jsonBytes))
Output: {"age":30,"name":"John"}
func (*Dict) Pop ¶
Pop method removes the key and its value from the dictionary. It returns the value for the given key, and an error if the key doesn't exist and no default value is provided.
Example ¶
d := Dict{"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", "default"))
Output: true 1 <nil> false <nil> the key is not found in the dict default <nil>
func (*Dict) PopItem ¶
PopItem method removes and returns an arbitrary key-value pair from the dictionary, along with an error if the dictionary is empty.
func (*Dict) Set ¶
Set method sets the value for the given key in the dictionary and returns the updated dictionary.
Example ¶
d := make(Dict)
fmt.Println(d.Get("a", nil))
fmt.Println(d.Has("a"))
d.Set("a", 5)
fmt.Println(d.Has("a"))
fmt.Println(d.Get("a", nil))
d.Set("a", 10)
fmt.Println(d.Get("a", nil))
Output: <nil> false true 5 10
func (Dict) Size ¶
Size returns the number of key-value pairs in the dictionary.
Example ¶
d := Dict{"one": 1, "two": 2, "three": 3}
fmt.Println(d.Size())
Output: 3
func (*Dict) UnmarshalJSON ¶
UnmarshalJSON converts the JSON data to a dictionary.
Example ¶
jsonStr := []byte(`{"age":30,"name":"John"}`)
var dict Dict
_ = json.Unmarshal(jsonStr, &dict)
fmt.Println(dict)
Output: map[age:30 name:John]
func (*Dict) Update ¶
Update method updates the dictionary with key-value pairs from another dictionary and returns the updated dictionary.
Example ¶
d1 := Dict{"a": 1, "b": 2}
d2 := Dict{"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]