Documentation
¶
Overview ¶
Package collections provides methods to operate on collections of values (structs/maps/arrays)
Index ¶
- func ConvertStructToMap(input []string) map[string]bool
- func ConvertStructToSyncMap(input []string) *sync.Map
- func ConvertStructToSyncMapWithCallback(input []string, callback func(inputItem string) (string, bool)) *sync.Map
- func ExtractMapValues(inputMap map[string]interface{}) []interface{}
- func GetMapValueOrError(input map[string]string, key string) (string, error)
- func JoinMap(inputMap map[string]string, sep string) (keysStr, valuesStr string)
- func MapToSlices(inputMap map[string]string) (keys, values []string)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConvertStructToMap ¶
ConvertStructToMap converts []string{"red", "blue", "red"} to map[string]bool{"red":true,"blue":true}
func ConvertStructToSyncMap ¶
ConvertStructToSyncMap converts []string{"red", "blue"} to sync.Map{"red":true,"blue":true}
func ConvertStructToSyncMapWithCallback ¶
func ConvertStructToSyncMapWithCallback(input []string, callback func(inputItem string) (string, bool)) *sync.Map
* ConvertStructToSyncMapWithCallback does the same as ConvertStructToSyncMap with additional filter/mutator callback * callback result string should return a modified value, result bool if false item won't be included in the output
Example ¶
This example converts struct to sync map with additional applying of callback function to modify and filter elements
//Want to have unique map of file names without extension and without '.' and '..'
//something like sync.Map{"file":true}
collectedFileNames := []string{".", "..", "file.txt", "file.txt"}
resultMap := ConvertStructToSyncMapWithCallback(
collectedFileNames,
func(inputItem string) (modifiedString string, shouldBeIncluded bool) {
if inputItem == "." || inputItem == ".." {
return "", false
}
return strings.Split(inputItem, ".")[0], true
},
)
printableMap := map[string]bool{}
resultMap.Range(func(key, value interface{}) bool {
printableMap[key.(string)] = value.(bool)
return true
})
fmt.Printf("%+v", printableMap)
Output: map[file:true]
func ExtractMapValues ¶
func ExtractMapValues(inputMap map[string]interface{}) []interface{}
ExtractMapValues converts map[string]interface{}{"Bob":"Bob", "Alice":"Alice", "John":"John"} by filter values []interface{}{"Bob","Alice"} to []interface{}{"Bob", "Alice", "John"}
func GetMapValueOrError ¶
GetMapValueOrError returns error if map doesn't contain the provided key
func JoinMap ¶
JoinMap converts map[string]string{"name":"Bob","color":"red","size","big"} to 2 strings "name,color,size" and "Bob,red,big" if "," is provided as separator
func MapToSlices ¶
MapToSlice converts map[string]string{"name":"Bob","color":"red","size","big"} to keys and values slices like []string{"name","color","size"} and []string{"Bob","red","big"}
Types ¶
This section is empty.