I am new to go, and I have found myself writing a few for
loops which look like this:
for element, err := producer.Produce(); err == nil; element, err = producer.Produce() {
process(element)
}
where producer.Produce()
is a function like reader.ReadString(' ')
or fmt.Fscan(Reader, &token)
. I would much rather like to write
for element := range elements {
process(element)
}
but for now, I would be satisfied to know if there is a cleaner way to iterate over the output of these kinds of functions in go. In particular, is there a nice way to get rid of this annoying duplication in the init statement and the post statement of the for statement?
I don't think there's anything quite as clean as what you're looking for. The idiomatic way to write it is:
for {
element, err := producer.Produce()
if err != nil {
break
}
process(element)
}
Here's a way to create a channel from such a method:
elements := make(chan elementType)
val send func()
send = func() {
if element, err := producer.Produce(); err == nil {
elements <- element
go send()
} else {
close(elements)
}
}
go send()
for element := range elements {
process(element)
}