Documentation
¶
Index ¶
- Constants
- func Delete(d Dict, key string)
- func Fetch[T any](d Dict, key string, defaultValue T) T
- func Get(d Dict, key string, defaultValue any) any
- func GetFloat(d Dict, key string, defaultValue float64) float64
- func GetInt(d Dict, key string, defaultValue int) int
- func GetString(d Dict, key string, defaultValue any) string
- func GetUint(d Dict, key string, defaultValue uint) uint
- func IsExist(d Dict, key string) bool
- func Keys(d Dict) []string
- func KeysN(d Dict, n int) []string
- func Merge(src, updt Dict)
- func Set(d Dict, key string, newValue any)
- func String(d Dict) string
- type Dict
Examples ¶
Constants ¶
const Separator = "."
Key separator character used for nested keys
Variables ¶
This section is empty.
Functions ¶
func Delete ¶
Delete removes a key from the dictionary if it exists. It supports nested keys using the separator.
Example ¶
package main import ( "fmt" "github.com/exonlabs/go-utils/pkg/abc/dictx" ) func main() { d := dictx.Dict{ "a": dictx.Dict{ "b": dictx.Dict{ "c": "value", }, }, } // Delete a key dictx.Delete(d, "a.b.c") // Check if the key was deleted exists := dictx.IsExist(d, "a.b.c") fmt.Println(exists) }
Output: false
func Fetch ¶
Fetch retrieves a value from the dictionary by key with type casting conversion. If the key is not found, the defaultValue is returned.
Example ¶
package main import ( "fmt" "github.com/exonlabs/go-utils/pkg/abc/dictx" ) func main() { d := dictx.Dict{ "a": dictx.Dict{ "b": dictx.Dict{ "c": 42, }, }, } // Fetch value with correct type (int) val := dictx.Fetch(d, "a.b.c", 0) fmt.Println(val) // Fetch value that doesn't exist (should return default value) val = dictx.Fetch(d, "a.b.d", 0) fmt.Println(val) }
Output: 42 0
func Get ¶
Get retrieves a value from the dictionary by key. If the key is not found, the defaultValue is returned.
Example ¶
package main import ( "fmt" "github.com/exonlabs/go-utils/pkg/abc/dictx" ) func main() { d := dictx.Dict{ "a": dictx.Dict{ "b": dictx.Dict{ "c": "value", }, }, } // Get an existing key val := dictx.Get(d, "a.b.c", "default") fmt.Println(val) // Get a non-existing key, return default value val = dictx.Get(d, "a.b.x", "default") fmt.Println(val) }
Output: value default
func GetFloat ¶
GetFloat retrieves a float value from the dictionary by key. If the key is not found, the defaultValue is returned.
func GetInt ¶
GetInt retrieves an integer value from the dictionary by key. If the key is not found, the defaultValue is returned.
func GetString ¶
GetString retrieves a value as string from the dictionary by key. If the key is not found, the defaultValue is returned.
func GetUint ¶
GetUint retrieves an unsigned integer value from the dictionary by key. If the key is not found, the defaultValue is returned.
func IsExist ¶
IsExist checks if a key exists in the dictionary. It supports nested keys using the separator. Returns true if the key exists, false otherwise.
Example ¶
package main import ( "fmt" "github.com/exonlabs/go-utils/pkg/abc/dictx" ) func main() { d := dictx.Dict{ "a": dictx.Dict{ "b": dictx.Dict{ "c": "value", }, }, } // Check if keys exist fmt.Println(dictx.IsExist(d, "a.b.c")) // true fmt.Println(dictx.IsExist(d, "a.b.d")) // false fmt.Println(dictx.IsExist(d, "a.x")) // false }
Output: true false false
func Keys ¶
Keys returns a list of all keys in the dictionary, regardless of nesting levels. It omits zero-length keys.
func KeysN ¶
KeysN returns a list of keys up to N levels nested. If n is 1, only top-level keys are returned. If n is greater than 1, it retrieves nested keys accordingly. Zero-length keys are omitted from the results.
Example ¶
package main import ( "fmt" "github.com/exonlabs/go-utils/pkg/abc/dictx" ) func main() { d := dictx.Dict{ "a": dictx.Dict{ "b": dictx.Dict{ "c": dictx.Dict{ "d": "value", }, }, }, } // Retrieve all keys with unlimited depth keys := dictx.KeysN(d, -1) fmt.Println(keys) // Retrieve top-level keys only keysTopLevel := dictx.KeysN(d, 1) fmt.Println(keysTopLevel) }
Output: [a.b.c.d] [a]
func Merge ¶
func Merge(src, updt Dict)
Merge updates a source dictionary recursively with an update dictionary. It merges keys and values, allowing nested dictionaries to be updated as well.
Example ¶
package main import ( "fmt" "github.com/exonlabs/go-utils/pkg/abc/dictx" ) func main() { src := dictx.Dict{ "a": dictx.Dict{ "b": "old_value", }, } updt := dictx.Dict{ "a": dictx.Dict{ "b": "new_value", "c": "new_key", }, } // Merge the update dict into the source dict dictx.Merge(src, updt) // Print the updated source dictionary fmt.Println(src) }
Output: map[a:map[b:new_value c:new_key]]
func Set ¶
Set adds a new value in the dictionary by key. If the key already exists, its value is overwritten.
Example ¶
package main import ( "fmt" "github.com/exonlabs/go-utils/pkg/abc/dictx" ) func main() { d := dictx.Dict{} // Set a nested value dictx.Set(d, "a.b.c.d", "new_value") // Get the value to confirm it was set correctly val := dictx.Get(d, "a.b.c.d", "default") fmt.Println(val) }
Output: new_value
Types ¶
type Dict ¶
Dict type representation as a map with string keys and any values
func Clone ¶
Clone creates a deep copy of a Dict. It returns a new dictionary that is a copy of the original, preserving the structure and values.
Example ¶
package main import ( "fmt" "github.com/exonlabs/go-utils/pkg/abc/dictx" ) func main() { d := dictx.Dict{ "a": dictx.Dict{ "b": "value", }, } cloned, err := dictx.Clone(d) if err != nil { fmt.Println("error:", err) return } // Print original and cloned dictionary fmt.Println("Original:", d) fmt.Println("Cloned:", cloned) }
Output: Original: map[a:map[b:value]] Cloned: map[a:map[b:value]]