doulong4169 2017-08-31 19:54
浏览 93
已采纳

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

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.

  • 写回答

2条回答 默认 最新

  • doulan2827 2017-08-31 20:16
    关注

    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() {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器