I am trying to build an array with go channels. I do not care about insertion order however I only receive the last item from the channel.
package main
import (
"fmt"
)
func AddToMap(thing string, val string, c chan map[string]string) {
mappy := make(map[string]string)
mappy[thing] = val
c <- mappy
}
func main() {
item := make([]map[string]string, 0, 10)
list1 := []string{"foo", "bar", "baz", "blah", "barf"}
list2 := []string{"one", "two", "three", "four", "five"}
c := make(chan map[string]string)
for index, val := range list1 {
go AddToMap(val, list2[index], c)
}
ret := <-c
item = append(item, ret)
fmt.Println(item)
}
My output is: [map[barf:five]]