dict

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: May 23, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package dict provides a flexible hash map implementation.

Index

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]) Clear

func (d *Dict[K, V]) Clear() *Dict[K, V]

Clear removes all key-value pairs from the dictionary.

func (Dict[K, V]) Copy

func (d Dict[K, V]) Copy() Dict[K, V]

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

func (d *Dict[K, V]) Delete(key K) bool

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

func (d Dict[K, V]) Empty() bool

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

func (d Dict[K, V]) Equal(another Dict[K, V]) bool

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

func (d Dict[K, V]) Has(key K) bool

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

func (d Dict[K, V]) Items() []*DictItem[K, V]

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

func (d Dict[K, V]) MarshalJSON() ([]byte, error)

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

func (d *Dict[K, V]) Pop(key K, args ...V) (value V, err error)

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

func (d *Dict[K, V]) PopItem() (key K, value V, err error)

PopItem removes and returns an arbitrary key-value pair from the dictionary.

func (*Dict[K, V]) Set

func (d *Dict[K, V]) Set(key K, value V) *Dict[K, V]

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

func (d Dict[K, V]) Size() int

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

func (d *Dict[K, V]) UnmarshalJSON(data []byte) (err error)

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

func (d *Dict[K, V]) Update(another Dict[K, V]) *Dict[K, V]

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]

func (Dict[K, V]) Values

func (d Dict[K, V]) Values() []V

Values returns a slice containing all the values in the dictionary.

type DictItem

type DictItem[K comparable, V any] struct {
	Key   K
	Value V
}

DictItem represents a key-value pair in the dictionary.

Jump to

Keyboard shortcuts

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