The Go Playground

The Go Playground Imports embed package main import ( "fmt" ) type PriorityQueue struct { high chan string low chan string } func (pq *PriorityQueue) Run() { for { if len(pq.high) == 0 && len(pq.low) == 0 { // this is just to prevent sleeping goroutine deadlocks in the Playgound // (since we aren't ever adding anything else to the queue). return } if len(pq.high) > 0 { pq.handle(<-pq.high) continue } select { case item := <-pq.high: pq.handle(item) case item := <-pq.low: pq.handle(item) } } } func (pq *PriorityQueue) handle(item string) { fmt.Println(item) } func NewPriorityQueue() *PriorityQueue { return &PriorityQueue{ high: make(chan string, 10), low: make(chan string, 10), } } func main() { pq := NewPriorityQueue() pq.low <- "I'm first, at LOW 1" pq.low <- "LOW 2" pq.high <- "HIGH 1" pq.high <- ...

Linked on 2019-11-25 21:55:21 | Similar Links