Queues - bali-nebula/go-component-framework GitHub Wiki
Overview
A queue is a collection that supports first-in-first-out (FIFO) semantics. Each value in the queue is a component. Queues allow multi-threaded access.
A Quick Example
To whet your appetite, here is some short example code that demonstrates the behavior of queues.
package main
import (
fmt "fmt"
syn "sync"
abs "github.com/bali-nebula/go-component-framework/v2/abstractions"
bal "github.com/bali-nebula/go-component-framework/v2/bali"
)
func main() {
// Create a new queue from a string containing Bali Document Notation™.
var queue = bal.Queue(`[
"first"
"second"
"third"
]($type: /bali/types/collections/Queue/v1)`)
fmt.Println("The initial queue:", bal.FormatEntity(queue))
// Create a wait group for synchronization.
var wg = new(syn.WaitGroup)
defer wg.Wait()
// Add another value to the queue.
queue.AddValue(bal.Component(`"fourth"`))
fmt.Println("The augmented queue:", bal.FormatEntity(queue))
// Remove values from the queue in the background.
wg.Add(1)
go func() {
defer wg.Done()
var value abs.ComponentLike
value, _ = queue.RemoveHead()
fmt.Println("The first item from the queue:", bal.FormatComponent(value))
value, _ = queue.RemoveHead()
fmt.Println("The second item from the queue:", bal.FormatComponent(value))
value, _ = queue.RemoveHead()
fmt.Println("The third item from the queue:", bal.FormatComponent(value))
value, _ = queue.RemoveHead()
fmt.Println("The fourth item from the queue:", bal.FormatComponent(value))
value, _ = queue.RemoveHead()
fmt.Println("The fifth item from the queue:", bal.FormatComponent(value))
fmt.Println("The queue is now empty:", bal.FormatEntity(queue))
}()
// Add one more value to the queue.
queue.AddValue(bal.Component(`"fifth"`))
queue.CloseQueue()
fmt.Println("The current queue:", bal.FormatEntity(queue))
}