I've question. I need to make a program running in the background. The program is to collect and save data in my database.
I started to do so:
func main() {
for {
doAll()
}
}
And that retrieves data from all addresses at one time ("go" function):
func doAll() {
rows, err := MySQL.QueryRow("SELECT * FROM `settings`")
checkError(err)
defer rows.Close()
for rows.Next() {
c := make(chan bool)
var http string
err = rows.Scan(&http )
checkError(err)
go doOne(http)
<- c
}
}
And that retrieves data from one web site.
func doOne() {
// some code
c <- true
}
My question is whether iterations of the "doAll" function will be in order?
Yes the iterations of doAll
will be in order, because the c
channel is unbuffered. That means in your for rows.Next()
loop, the read from c
will wait until doOne
writes to the channel.
You can make this code simpler by removing the channel and executing doOne
synchronously (that is: just call it as a function). The code refactored in this way has exactly the same semantics.