I'm trying to range over channels in a map via key. This is the map:
documentChansPerHost := make(map[string](chan Document))
This is how I want to range over them:
wg := sync.WaitGroup{}
documentsCountPerHost := make(map[string]int)
// Count documents per host
for _,document := range documents {
documentsCountPerHost[document.Host]++
}
// Spawn routines for up to 5 times the number of different
for host,documentCount := range documentsCountPerHost {
println("Spawning " + strconv.Itoa(HOST_CONCURRENCY_LIMIT) + " Routines for Host " + host + " for " + strconv.Itoa(documentCount) + " url(s)")
for i := 0; i < HOST_CONCURRENCY_LIMIT; i++ {
go func() {
for document := range documentChansPerHost[host] {
fmt.Println(document.Url)
wg.Done()
}
}
}
}
And that's how I create, add and close (to) the channels
// Instantiate channels for documents per host
for host,_ := range documentsCountPerHost {
documentChansPerHost[host] = make(chan Document, 20)
}
for _, document:= range documents {
wg.Add(1)
documentChansPerHost[document.Host] <- document
}
for _, documentChan := range documentChansPerHost {
close(documentChan)
}
wg.Wait()
But for some reason it does not output all document Urls I have passed into the channels and the program never ends. Am I using the channel maps in a wrong way or has that something to do with the concurrency and locks somewhere?