I have the following code snippet. I create a channel that takes 15 filenames at most from a given directory. I thought that I could create goroutines where one produces entries on a channel, and another consumes them. The consumer should print things taken from the channel.
My program executes without printing and I suspect that this is because the consumer routine is sleeping - isn't a new go routine started for each iteration of the for-loop? Shouldn't there eventually be something to print from the channel?
func (u* uniprot) produce(n string) {
u.namesInDir <- n
}
func (u* uniprot) consume() {
fmt.println(<-u.namesInDir)
}
func (u* uniprot) readFilenames(dirname string) {
u.namesInDir = make(chan string, 15)
dir, err := os.Open(dirname)
errorCheck(err)
names, err := dir.Readdirnames(0)
errorCheck(err)
for _, n := range names {
go u.produce(n)
go u.consume()
}
}