collections

package
v1.3.7 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2020 License: MPL-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package collections provides methods to operate on collections of values (structs/maps/arrays)

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertStructToMap

func ConvertStructToMap(input []string) map[string]bool

ConvertStructToMap converts []string{"red", "blue", "red"} to map[string]bool{"red":true,"blue":true}

func ConvertStructToSyncMap

func ConvertStructToSyncMap(input []string) *sync.Map

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

func GetMapValueOrError(input map[string]string, key string) (string, error)

GetMapValueOrError returns error if map doesn't contain the provided key

func JoinMap

func JoinMap(inputMap map[string]string, sep string) (keysStr, valuesStr string)

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

func MapToSlices(inputMap map[string]string) (keys, values []string)

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.

Jump to

Keyboard shortcuts

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