func ExampleQueue_subscribe() { // Create a new queue. q := queue.New() // The number of data we'll add and get from the queue. const n = 2 // Waitgroup used in waiting for the concurrent getters. var wg sync.WaitGroup wg.Add(1) go func() { // The loop and call to Queue.Get(true) below will act like a subscribe // function. The call to Queue.Get with blocking = true will block until // more data is available. // // Normally this would loop forever, but since we know the number of data // thats get added to the queue, we can stop after we got all data. for i := 1; i <= n; i++ { data := q.Get(true) fmt.Printf("Got data: %s\n", data) } wg.Done() }() // Fake some busy work. time.Sleep(100 * time.Millisecond) // Add our data to the queue. for i := 1; i <= n; i++ { data := []byte(fmt.Sprintf("Hello world %d", i)) q.Add(data) } // Make sure the data gets retrieved from the queue and printed. wg.Wait() // Output: // Got data: Hello world 1 // Got data: Hello world 2 }
func ExampleQueue() { // Create a new queue. q := queue.New() // Get data from the queue, not blocking the call. Since the queue is empty // we'll get nil. data := q.Get(false) fmt.Printf("Got nil: %t\n", data == nil) // Add data to the queue. // // It's important to note that data passed to Queue.Add may no longer be // modified. q.Add([]byte("Hello world")) // Try getting data again, this time it should be the data we just added to // queue. data = q.Get(false) fmt.Printf("Got data: %q\n", data) // Output: // Got nil: true // Got data: "Hello world" }