P
P
Pavel Shvedov2014-12-17 15:01:59
go
Pavel Shvedov, 2014-12-17 15:01:59

Another incomprehensible situation with Go?

Hello everyone, straight to the point. The code:

for _ = range time.Tick(3 * time.Second) {
    fmt.Println("Making query")
    rows, err := db.Query("SELECT id, addr, method FROM tasks")
    defer rows.Close()
    if err != nil {
      fmt.Printf("Database error: %s\n", err.Error())
      os.Exit(1)
    } else {
      fmt.Println("Filling queue")
      queue := make(chan *Task)
      for rows.Next() {
        fmt.Println("Get next row")
        var id int32
        var addr string
        var method string
        if err := rows.Scan(&id, &addr, &method); err != nil {
          fmt.Printf("Row error: %s\n", err.Error())
        } else {
          fmt.Printf("Add task for: %s\n", addr)
          queue <- &Task{id, addr, method}
          fmt.Println("Task added")
        }
      }
      fmt.Println("Queue filled")
      close(queue)
      process(queue)
    }
  }

I read rows from the database, at the first iteration of rows.Next(), the data is normally retrieved, but when I push the result into the channel, everything just gets up at this step.
queue <- &Task{id, addr, method}
What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SilentFl, 2014-12-17
@mmmaaak

As in your previous question - blocking due to an unbuffered channel.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question