amuz.es/src/go/misc@v1.0.1/q/bytes_queue.go (about) 1 package q 2 3 import "container/list" 4 5 // NewBytesQueue creates a non blocking bytearray queue channel. 6 func NewBytesQueue() (chan<- []byte, <-chan []byte) { 7 send := make(chan []byte, 1) 8 receive := make(chan []byte, 1) 9 go manageBytesQueue(send, receive) 10 return send, receive 11 } 12 13 func manageBytesQueue(send <-chan []byte, receive chan<- []byte) { 14 queue := list.New() 15 defer close(receive) 16 for { 17 if front := queue.Front(); front == nil { 18 if value, ok := <-send; ok { 19 queue.PushBack(value) 20 } else { 21 break 22 } 23 } else { 24 select { 25 case receive <- front.Value.([]byte): 26 queue.Remove(front) 27 case value, ok := <-send: 28 if ok { 29 queue.PushBack(value) 30 } 31 } 32 } 33 } 34 }