I'd like to pass a map through a channel in go:
func main() {
var pipe map[string]string
pipe = make(chan map[string]string, 2)
go connect("myhost", "100", pipe)
out := <-pipe
...}
so that the func() passes response and error through the channel:
func connect(host string, url string, pipe chan<- map[string]string) {
fmt.Println("Trying "+url)
var lpipe map[string]string
lpipe = make(map[string]string)
lpipe["resp"], lpipe["err"] = "aaa","bbb"
pipe <- lpipe
}
The compiler rejects both
pipe = make(chan map[string]string, 2)
:cannot use make(chan map[string]string, 2) (type chan map[string]string) as type map[string]string in assignment
and
pipe = make(chan map, 2)
:unexpected comma, expecting [
What am I doing wrong? The whole thing is for being parallelized into goroutines.
Thank you
The error message tells you exactly what you're doing wrong: You're trying to assign a value of type chan map[string]string
to a variable of type map[string]string
:
var pipe map[string]string
pipe = make(chan map[string]string, 2)
Here you're creating a variable called pipe
of type map[string]string
, then on the second line, you're creating a channel (of type chan map[string]string
) and trying to assign it to that variable. That doesn't work.
You're doing the same thing in your second code example.
map[string]string
and chan map[string]string
are distinct types.