Is there an easy/simple means of converting a slice into a map in Golang? Like converting an array into hash in perl is easy to do with simple assignment like %hash =
If looking for a library you can use go-funk. This code may be less performant and not idiomatic in Go until we have Generics.
var elements = []string{"abc", "def", "fgi", "adi"}
elementsMap := funk.Map(
funk.Chunk(elements, 2),
func(x []string) (string, string) { // Slice to Map
return x[0], x[1]
},
)
fmt.Println(elementsMap) // map[abc:def fgi:adi]
https://play.golang.org/p/-t-33z4aKM_j