github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/queues/chan.go (about)

     1  // implements fifo using a channel
     2  package goqueuestest
     3  
     4  type CFifo struct {
     5  	ch chan interface{}
     6  }
     7  
     8  func NewChanFifo(size int) *CFifo {
     9  	return &CFifo{make(chan interface{}, size)}
    10  }
    11  
    12  func (q *CFifo) Enqueue(value interface{}) {
    13  	q.ch <- value
    14  }
    15  
    16  func (q *CFifo) Dequeue() (value interface{}, ok bool) {
    17  	select {
    18  	case value, ok = <-q.ch:
    19  		return value, ok
    20  	default:
    21  	}
    22  	return nil, false
    23  }