Documentation
¶
Overview ¶
Package maps provides utility functions for working with maps and structs. It includes functions for converting between maps and structs, as well as retrieving nested values from maps using dot notation.
This package is particularly useful when dealing with dynamic data structures or when working with configuration data that needs to be converted between different formats.
Key features: - Map2Struct: Converts a map to a struct using reflection - Get: Retrieves nested values from maps using dot notation - Support for weakly typed input when converting maps to structs - Handling of time.Duration conversions from string representations
Usage example:
input := map[string]interface{}{
"name": "John Doe",
"age": 30,
"address": map[string]interface{}{
"street": "123 Main St",
"city": "Anytown",
},
}
// Retrieve a nested value
city := maps.Get(input, "address.city")
// Convert map to struct
type Person struct {
Name string
Age int
Address struct {
Street string
City string
}
}
var person Person
err := maps.Map2Struct(input, &person)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Get ¶
func Get(input interface{}, fieldName string) interface{}
Get 获取map中的字段,支持嵌套结构获取,例如fieldName.subFieldName.xx 嵌套类型必须是map[string]interface{} 如果字段不存在,返回nil
func Map2Struct ¶
Map2Struct Decode takes an input structure and uses reflection to translate it to the output structure. output must be a pointer to a map or struct.
func Struct2Map ¶
Types ¶
This section is empty.