从另一个队列中轮询一个简单队列,并从第二个队列填充第一个队列

In other words, the flow of an event-driven queue:

  • poll event queue
  • if event found, process event, then next poll cycle
  • if no event found, poll data queue
  • if data found, push to event queue, start next cycle polling event queue
  • if no data found, exit flow

Here is the code:

package main

import "fmt"

type Queue struct {
    stream []string
}

// Next returns first element from stream.
// Returns false if no element is in the stream.
func (q *Queue) Next() (s string, ok bool) {
    if len(q.stream) == 0 {
        return s, false
    }
    s = q.stream[0]
    q.stream = q.stream[1:]
    return s, true
}

func main() {
    // define queues
    events := []string{"event"}
    q1 := &Queue{stream: events}
    data := []string{"data1", "data2", "data3"}
    q2 := &Queue{stream: data}

    // poll first queue
    for e, ok := q1.Next(); ok; e, ok = q1.Next() {
        // nothing in q1
        if !ok {
            fmt.Println("Nothing found in q1")

            // poll second queue
            d, ok := q2.Next()
            if !ok {
                return
            }

            // found d in q2 and append to q1
            fmt.Printf("found in q2 %v
", d)
            q1.stream = append(q1.stream, d)
            return
        }

        // do some stuff to e ...
        fmt.Printf("found in q1 %v %v
", e, ok)
    }
}

Try it in the playground https://play.golang.org/p/bgJzq2hCfl.

All I get is the element in the first queue.

found in q1 event true

The code never polls the second queue and append its elements to the first queue. What do i wrong?

If I try

// poll first queue
for e, ok := q1.Next() {
    // ...
}

the compiler throws

tmp/sandbox299553905/main.go:28:25: syntax error: e, ok := q1.Next() used as value

Any hints appreciated.


Update: Here is the solution to the loop.

// poll first queue
for e, ok := q1.Next(); true; e, ok = q1.Next() {
    // nothing in q1
    if !ok {
        fmt.Println("Nothing found in q1")

        // poll second queue
        for d, ok := q2.Next(); true; d, ok = q2.Next() {
            if !ok {
                fmt.Println("Nothing found in q2. exit")
                return
            }

            // found d in q2 and append to q1
            fmt.Printf("found in q2: %v. append to q1
", d)
            q1.stream = append(q1.stream, d)
            break
        }
        continue
    }

    // do some stuff to e ...
    fmt.Printf("found in q1: %v. do stuff
", e)
}

You can test it in the playground https://play.golang.org/p/qo0zoBPROe.

You exit the loop when it doesn't find anything. Just change your for loop to the following, but note that this makes the loop infinite, so you have to break out or return to get out.

for e, ok := q1.Next(); ok || !ok ; e, ok = q1.Next() {

After the first iteration, the "increment" happens and this time ok is set to false. The next step compares ok == true (loop termination condition) which is not the case and thus exits.

Now, if I understand correctly what you try to do, I would modify the loop to:

// poll first queue
for e, ok := q1.Next(); true; e, ok = q1.Next() {
    // nothing in q1
    if !ok {
        fmt.Println("Nothing found in q1")

        // poll second queue
        d, do := q2.Next()
        if !do {
            break
        }

        // found d in q2 and append to q1
        fmt.Printf("found in q2 %v
", d)
        q1.stream = append(q1.stream, d)
    } else {

        // do some stuff to e ...
        fmt.Printf("found in q1 '%v' ok=%v
", e, ok)
    }
}