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 map[any]any

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

func FromEntries(entries ...[2]any) Dict

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

func (d *Dict) Clear() *Dict

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

func (d Dict) Copy() Dict

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

func (d *Dict) Delete(key any) bool

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

func (d Dict) Empty() bool

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

func (d Dict) Equal(another Dict) bool

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

func (d Dict) Get(key any, defaultValue ...any) (value any)

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

func (d Dict) Has(key any) bool

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

func (d Dict) Items() [][2]any

Items returns a slice containing all the key-value pairs present in the dictionary.

func (Dict) Keys

func (d Dict) Keys() []any

Keys returns a slice containing all the keys present in the dictionary.

func (Dict) MarshalJSON

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

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

func (d *Dict) Pop(key any, args ...any) (value any, err error)

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

func (d *Dict) PopItem() (key, value any, err error)

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

func (d *Dict) Set(key, value any) *Dict

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

func (d Dict) Size() int

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

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

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

func (d *Dict) Update(another Dict) *Dict

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]

func (Dict) Values

func (d Dict) Values() []any

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

Jump to

Keyboard shortcuts

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