-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add queue go-reference example usage
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
# Example | ||
Basic usage: | ||
q := New[string]() | ||
q.Enqueue("Foo") | ||
item, ok := q.Dequeue() | ||
if !ok { | ||
panic("item should be dequeued") | ||
} | ||
fmt.Println(item) // Output: Foo | ||
Concurrency usage: | ||
q := New[string]() | ||
go func() { | ||
for { | ||
item, ok := q.Dequeue() | ||
if !ok { | ||
break | ||
} | ||
fmt.Println(item) | ||
} | ||
}() | ||
data := []string{"Foo", "Bar", "Baz"} | ||
for _, item := range data { | ||
q.Enqueue(item) | ||
} | ||
q.Close() // close queue to stop goroutine | ||
Queue is unlimited capacity, so you can enqueue as many as you want without blocking or dequeue required: | ||
q := New[string]() | ||
for i := 0; i < 1000000; i++ { | ||
q.Enqueue("Foo") | ||
} | ||
*/ | ||
package queue |