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('\n')
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)
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments