├── go.mod ├── LICENSE ├── examples └── examples.go ├── README.md ├── testing └── testing.go ├── queue.go └── generic └── generic_queue.go /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/realTristan/goqueue 2 | 3 | go 1.18 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Tristan Simpson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /examples/examples.go: -------------------------------------------------------------------------------- 1 | package queue_examples 2 | 3 | import ( 4 | Queue "github.com/realTristan/goqueue" 5 | ) 6 | 7 | // Add Items to the queue 8 | func AddItems() { 9 | queue := Queue.Create() 10 | 11 | // Add items from a slice to the queue 12 | items := [3]interface{}{1.1, 1, "String"} 13 | for _, item := range items { 14 | queue.Put(item) 15 | } 16 | } 17 | 18 | // Remove items from the queue 19 | func RemoveItems() { 20 | queue := Queue.Create() 21 | 22 | // Remove at index 23 | removedItem := queue.RemoveAtIndex(0) 24 | println(removedItem) 25 | 26 | // Search and remove 27 | queue.Remove("Item") 28 | } 29 | 30 | // Get items from the queue 31 | func GetItems() { 32 | queue := Queue.Create() 33 | 34 | // Get the item from the queue (doesn't remove it from the queue) 35 | item := queue.Get() 36 | println(item) 37 | 38 | // Grab the item from the queue (removes it from the queue) 39 | _item := queue.Grab() 40 | println(_item) 41 | } 42 | 43 | // Other Queue Functions 44 | func OtherFunctions() { 45 | queue := Queue.Create() 46 | if queue.Contains("Item") { 47 | println("Contains Item") 48 | } 49 | 50 | // Clear the queue 51 | queue.Clear() 52 | 53 | // Show the queue contents 54 | println(queue.Show()) 55 | 56 | // Get item at specific index 57 | // itemAtIndex := queue.GetAtIndex(1) 58 | 59 | // Returns whether queue is empty 60 | // isEmpty := queue.IsEmpty() 61 | 62 | // Returns whether queue is not empty 63 | // isNotEmpty := queue.IsNotEmpty() 64 | 65 | // Returns the length of the queue slice 66 | // queueLength := queue.Size() 67 | } 68 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # GoQueue ![Stars](https://img.shields.io/github/stars/realTristan/goqueue?color=brightgreen) ![Watchers](https://img.shields.io/github/watchers/realTristan/goqueue?label=Watchers) 3 | 4 | ![Go Queue Banner](https://user-images.githubusercontent.com/75189508/183435878-e5669071-df93-478a-a364-245862dadddb.png) 5 | 6 | Flexible Queue System for Go. 7 | 8 | # Why GoQueue? 9 | GoQueue is a light weight, easy to read open source module that uses solely native golang code. GoQueue's functions are based off of the python queue library so the learning curve is not as time consuming. 10 | 11 | # Installation 12 | ``` 13 | go get -u github.com/realTristan/goqueue 14 | ``` 15 | 16 | 17 | # Quick Usage 18 | ```go 19 | package main 20 | 21 | import ( 22 | "github.com/realTristan/goqueue" 23 | ) 24 | 25 | func main() { 26 | // Create a new queue 27 | queue := goqueue.Create() 28 | 29 | // Put item into the queue 30 | queue.Put("Item") 31 | 32 | // Get the item from the queue 33 | item := queue.Get() 34 | 35 | // Print the item 36 | println(*item) 37 | 38 | // Output -> "Item" 39 | } 40 | ``` 41 | 42 | # GoQueue Functions 43 | ```go 44 | 45 | // Create() -> *ItemQueue 46 | // The Create() function will return an empty ItemQueue 47 | func Create() *ItemQueue {} 48 | 49 | // q.Index(index integer) -> *Item 50 | // The RemoveAtIndex() function is used to remove an item at the provided index of the ItemQueue 51 | // The function will then return the removed item if the user requires it's use 52 | func (q *ItemQueue) RemoveAtIndex(i int) *Item {} 53 | 54 | // q.Contains(Item) -> None 55 | // The Contains() function will scheck whether the provided ItemQueue contains 56 | // the given Item (_item) 57 | func (q *ItemQueue) Contains(item Item) bool {} 58 | 59 | // q.Remove(Item) -> None 60 | // The Remove() function will secure the ItemQueue before iterating 61 | // through said ItemQueue and remove the given Item (_item) 62 | func (q *ItemQueue) Remove(item Item) {} 63 | 64 | // q.Put(Item) -> None 65 | // The Put() function is used to add a new item to the provided ItemQueue 66 | func (q *ItemQueue) Put(i Item) {} 67 | 68 | // q.Get() -> Item 69 | // The Get() function will append the first item of the ItemQueue to the back of the slice 70 | // then remove it from the front 71 | // The function returns the first item of the ItemQueue 72 | func (q *ItemQueue) Get() *Item {} 73 | 74 | // q.Grab() -> Item 75 | // The Grab() function will return the first item of the ItemQueue then 76 | // remove it from said ItemQueue 77 | func (q *ItemQueue) Grab() *Item {} 78 | 79 | // q.Clear() -> None 80 | // The Clear() function will secure the queue then remove all of its items 81 | func (q *ItemQueue) Clear() {} 82 | 83 | // q.Show() -> *[]Item 84 | // The Show() function will return the ItemQueue's items 85 | func (q *ItemQueue) Show() *[]Item {} 86 | 87 | // q.GetAtIndex(index integer) -> *Item 88 | // The GetAtIndex() function is used to return an item at the provided index of the ItemQueue 89 | func (q *ItemQueue) GetAtIndex(i int) *Item {} 90 | 91 | // q.IsEmpty() -> bool 92 | // The IsEmpty() function will return whether the provided ItemQueue contains any Items 93 | func (q *ItemQueue) IsEmpty() bool {} 94 | 95 | // q.IsNotEmpty() -> bool 96 | // The IsNotEmpty() function will return whether the provided ItemQueue contains any Items 97 | func (q *ItemQueue) IsNotEmpty() bool {} 98 | 99 | // q.Size() -> int 100 | // The Size() function will return the length of the ItemQueue slice 101 | func (q *ItemQueue) Size() int {} 102 | 103 | ``` 104 | 105 | # License 106 | MIT License 107 | 108 | Copyright (c) 2022 Tristan Simpson 109 | 110 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 111 | 112 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 113 | 114 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 115 | -------------------------------------------------------------------------------- /testing/testing.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "sync" 6 | 7 | Queue "github.com/realTristan/goqueue" 8 | ) 9 | 10 | // ThreadSafety() Function checks to make sure reading/writing the queue 11 | // 12 | // is thread safe 13 | func ThreadSafety(queue *Queue.ItemQueue) { 14 | // Track GoRoutines 15 | var ( 16 | count int = 0 17 | wg sync.WaitGroup = sync.WaitGroup{} 18 | ) 19 | wg.Add(1) 20 | 21 | // Create goroutines (100) 22 | for i := 0; i < 100; i++ { 23 | go func() { 24 | for { 25 | count++ 26 | 27 | // Put item in the queue 28 | queue.Put(count) 29 | 30 | // Get the item from the queue 31 | item := queue.Grab() 32 | 33 | // Print the item 34 | fmt.Println(*item) 35 | } 36 | }() 37 | } 38 | // Wait for goroutines to finish (never) 39 | wg.Wait() 40 | } 41 | 42 | // Testing: Show -> show queue contents 43 | func ShowQueueContents(queue *Queue.ItemQueue, test string) { 44 | queueContents := queue.Show() 45 | fmt.Printf("Testing: %s: Queue Contents: %v\n", test, queueContents) 46 | } 47 | 48 | // Testing: Put -> putting an item into the queue 49 | func TestPut(queue *Queue.ItemQueue) { 50 | 51 | // Show queue contents before put 52 | ShowQueueContents(queue, "Put") 53 | 54 | // Put item into queue 55 | queue.Put("Item") 56 | 57 | // Show queue contents after put 58 | ShowQueueContents(queue, "Put") 59 | } 60 | 61 | // Testing: GetAtIndex -> getting an item at a specific index 62 | func TestGetAtIndex(queue *Queue.ItemQueue) { 63 | 64 | // Get item from the queue at index: 1 65 | itemAtIndex := queue.GetAtIndex(1) 66 | fmt.Printf("Item at index 1: %d\n", itemAtIndex) 67 | } 68 | 69 | // Testing: Get -> getting an item from the queue 70 | func TestGet(queue *Queue.ItemQueue) { 71 | 72 | // Get item from the queue 73 | getItem := queue.Get() 74 | fmt.Printf("Get Item in queue: %v\n", *getItem) 75 | } 76 | 77 | // Testing: Grab -> grabbing an item from the queue 78 | func TestGrab(queue *Queue.ItemQueue) { 79 | 80 | // Show queue contents before grab 81 | ShowQueueContents(queue, "Grab") 82 | 83 | // Grab the item 84 | grabItem := queue.Grab() 85 | fmt.Printf("Grab Item in queue: %v\n", *grabItem) 86 | 87 | // Show queue contents after grab 88 | ShowQueueContents(queue, "Grab") 89 | } 90 | 91 | // Testing: IsEmpty -> checking whether queue is empty 92 | func TestIsEmpty(queue *Queue.ItemQueue) { 93 | 94 | // Check if queue is empty 95 | isEmpty := queue.IsEmpty() 96 | fmt.Printf("Is queue empty: %v\n", isEmpty) 97 | 98 | // Show whether queue is empty or not 99 | ShowQueueContents(queue, "IsEmpty") 100 | } 101 | 102 | // Testing: IsNotEmpty -> checking whether queue is not empty 103 | func TestIsNotEmpty(queue *Queue.ItemQueue) { 104 | 105 | // Check if queue is not empty 106 | isNotEmpty := queue.IsNotEmpty() 107 | fmt.Printf("Is queue not empty: %v\n", isNotEmpty) 108 | 109 | // Show whether queue is empty or not 110 | ShowQueueContents(queue, "IsNotEmpty") 111 | } 112 | 113 | // Testing: Size -> checking the length of the queue slice 114 | func TestSize(queue *Queue.ItemQueue) { 115 | 116 | // Get queue size 117 | queueLength := queue.Size() 118 | fmt.Printf("Length of queue: %d\n", queueLength) 119 | 120 | // Show queue contents to check whether size is correct 121 | ShowQueueContents(queue, "Size") 122 | } 123 | 124 | // Testing: Contains -> check if queue contains item 125 | func TestContains(queue *Queue.ItemQueue) { 126 | 127 | // Get whether queue contains "Item" 128 | contains := queue.Contains("Item") 129 | fmt.Printf("Contains \"item\": %v\n", contains) 130 | 131 | // Show queue contents to show whether it contains item or not 132 | ShowQueueContents(queue, "Contains") 133 | } 134 | 135 | // Testing: Clear -> clear item queue 136 | func TestClear(queue *Queue.ItemQueue) { 137 | // Put new item into the queue 138 | queue.Put("Item") 139 | 140 | // Show the queue 141 | ShowQueueContents(queue, "Clear") 142 | 143 | // Clear the queue 144 | queue.Clear() 145 | 146 | // Show the cleared queue 147 | ShowQueueContents(queue, "Clear") 148 | } 149 | 150 | // Testing: Remove -> remove item in the queue 151 | func TestRemove(queue *Queue.ItemQueue) { 152 | // Add new item to the queue 153 | queue.Put("Item") 154 | 155 | // Show item queue 156 | ShowQueueContents(queue, "Remove") 157 | 158 | // Remove item from the queue 159 | queue.Remove("Item") 160 | 161 | // Show item queue 162 | ShowQueueContents(queue, "Remove") 163 | 164 | } 165 | 166 | // Testing: RemoveAtIndex -> remove item at specific index 167 | func TestRemoveAtIndex(queue *Queue.ItemQueue) { 168 | // Add new item to the queue 169 | queue.Put("Item") 170 | 171 | // Show item queue 172 | ShowQueueContents(queue, "RemoveAtIndex") 173 | 174 | // Remove item from the queue 175 | queue.RemoveAtIndex(0) 176 | 177 | // Show item queue 178 | ShowQueueContents(queue, "RemoveAtIndex") 179 | } 180 | 181 | // Main function 182 | func main() { 183 | // Testing: Create -> creating a new queue 184 | var queue *Queue.ItemQueue = Queue.Create() 185 | ShowQueueContents(queue, "Create") 186 | 187 | ///////////////////////////// 188 | // Testing Functions 189 | // 190 | // ThreadSafety(queue) 191 | // TestPut(queue) 192 | // TestGetAtIndex(queue) 193 | // TestGet(queue) 194 | // TestGrab(queue) 195 | // TestIsEmpty(queue) 196 | // TestIsNotEmpty(queue) 197 | // TestSize(queue) 198 | // TestClear(queue) 199 | // TestRemove(queue) 200 | // TestRemoveAtIndex(queue) 201 | // 202 | ///////////////////////////// 203 | } 204 | -------------------------------------------------------------------------------- /queue.go: -------------------------------------------------------------------------------- 1 | package goqueue 2 | 3 | // Import sync package for mutexes 4 | import "sync" 5 | 6 | // Type Item interface{} 7 | // 8 | // The 'Item' Type is the type of variables that will be going inside the queue slice 9 | // The Item is declared as interface so it is possible to have multiple types 10 | // within the Queue Slice 11 | type Item interface{} 12 | 13 | // type ItemQueue struct 14 | // 15 | // The 'ItemQueue' Struct contains the []'Type Item interface{}' slice 16 | // This struct holds two keys, 17 | // - items -> the []'Type Item interface{}' slice 18 | // - mutex -> the mutex lock which prevents overwrites and data corruption 19 | // ↳ We use RWMutex instead of Mutex as it's better for majority read slices 20 | type ItemQueue struct { 21 | items []Item 22 | mutex *sync.RWMutex 23 | } 24 | 25 | // The Create() function is used to create 26 | // a new, empty ItemQueue 27 | func Create() *ItemQueue { 28 | return &ItemQueue{ 29 | mutex: &sync.RWMutex{}, 30 | items: []Item{}, 31 | } 32 | } 33 | 34 | // The RemoveAtIndex() function is used to remove an 35 | // item at the provided index of the ItemQueue 36 | // 37 | // Returns the removed item 38 | func (q *ItemQueue) RemoveAtIndex(i int) Item { 39 | // Mutex Locking/Unlocking 40 | q.mutex.Lock() 41 | defer q.mutex.Unlock() 42 | 43 | // Create a copy of the item 44 | var copy Item = q.items[i] 45 | 46 | // Remove the item at the specific index 47 | q.items = append(q.items[:i], q.items[i+1:]...) 48 | 49 | // Return the copied item 50 | return copy 51 | } 52 | 53 | // The Contains() function will check whether 54 | // the provided ItemQueue contains the given item 55 | func (q *ItemQueue) Contains(item Item) bool { 56 | // Mutex Locking/Unlocking 57 | q.mutex.RLock() 58 | defer q.mutex.RUnlock() 59 | 60 | // Iterate over the queue 61 | for i := 0; i < len(q.items); i++ { 62 | if q.items[i] == item { 63 | return true 64 | } 65 | } 66 | return false 67 | } 68 | 69 | // q.Remove(Item) -> None 70 | // The Remove() function will secure the ItemQueue before iterating 71 | // over the queue items, removing the given Item (_item) 72 | func (q *ItemQueue) Remove(item Item) { 73 | // Mutex Locking/Unlocking 74 | q.mutex.Lock() 75 | defer q.mutex.Unlock() 76 | 77 | // Iterate over the queue items 78 | for i := 0; i < len(q.items); i++ { 79 | if q.items[i] == item { 80 | q.items = append(q.items[:i], q.items[i+1:]...) 81 | return 82 | } 83 | } 84 | } 85 | 86 | // The Put() function is used to add a new 87 | // item to the provided ItemQueue 88 | func (q *ItemQueue) Put(item Item) { 89 | // Mutex Locking/Unlocking 90 | q.mutex.Lock() 91 | defer q.mutex.Unlock() 92 | 93 | // Append the item to the queue 94 | q.items = append(q.items, item) 95 | } 96 | 97 | // The Get() function will append the first 98 | // item of the ItemQueue to the front of the 99 | // slice then remove it from the back 100 | func (q *ItemQueue) Get() Item { 101 | // Mutex Locking/Unlocking 102 | q.mutex.Lock() 103 | defer q.mutex.Unlock() 104 | 105 | // Create a copy of the first item of the queue 106 | var copy Item = q.items[0] 107 | 108 | // Move the first item of the queue to the front 109 | q.items = append(q.items, q.items[0]) 110 | q.items = q.items[1:] 111 | 112 | // Return the item 113 | return copy 114 | } 115 | 116 | // The Grab() function will return the first item of the 117 | // queue items slikce then remove it from said slice 118 | func (q *ItemQueue) Grab() Item { 119 | // Mutex Locking/Unlocking 120 | q.mutex.Lock() 121 | defer q.mutex.Unlock() 122 | 123 | // Get the first item of the queue 124 | var item Item = q.items[0] 125 | 126 | // And remove it 127 | q.items = q.items[1:] 128 | 129 | // Return the item 130 | return item 131 | } 132 | 133 | // The Clear() function will secure the 134 | // queue then remove all of its items 135 | func (q *ItemQueue) Clear() { 136 | // Mutex Locking/Unlocking 137 | q.mutex.Lock() 138 | defer q.mutex.Unlock() 139 | 140 | // Remove all items from the queue 141 | q.items = []Item{} 142 | } 143 | 144 | // The Show() function will return the ItemQueue's items 145 | func (q *ItemQueue) Show() []Item { 146 | // Mutex Locking/Unlocking 147 | q.mutex.RLock() 148 | defer q.mutex.RUnlock() 149 | 150 | // Create a copy of the queue items 151 | var copy []Item = q.items 152 | 153 | // Return the copy 154 | return copy 155 | } 156 | 157 | // The GetAtIndex() function is used to return an item 158 | // at the provided index of the ItemQueue 159 | func (q *ItemQueue) GetAtIndex(i int) Item { 160 | // Mutex Locking/Unlocking 161 | q.mutex.RLock() 162 | defer q.mutex.RUnlock() 163 | 164 | // Create a copy of the item at the index 165 | var copy Item = q.items[i] 166 | 167 | // Return the copy 168 | return copy 169 | } 170 | 171 | // The IsEmpty() function will return whether the 172 | // provided ItemQueue contains any Items 173 | func (q *ItemQueue) IsEmpty() bool { 174 | 175 | // Mutex Locking/Unlocking 176 | q.mutex.RLock() 177 | defer q.mutex.RUnlock() 178 | 179 | // Return whether queue is empty 180 | return len(q.items) == 0 181 | } 182 | 183 | // The IsNotEmpty() function will return whether 184 | // the provided ItemQueue contains any Items 185 | func (q *ItemQueue) IsNotEmpty() bool { 186 | // Mutex Locking/Unlocking 187 | q.mutex.RLock() 188 | defer q.mutex.RUnlock() 189 | 190 | // Return whether length is greater than 0 191 | return len(q.items) > 0 192 | } 193 | 194 | // The Size() function will return the 195 | // length of the ItemQueue slice 196 | func (q *ItemQueue) Size() int { 197 | // Mutex Locking/Unlocking 198 | q.mutex.RLock() 199 | defer q.mutex.RUnlock() 200 | 201 | // Return the queue length 202 | return len(q.items) 203 | } 204 | -------------------------------------------------------------------------------- /generic/generic_queue.go: -------------------------------------------------------------------------------- 1 | package generic_queue 2 | 3 | import "sync" 4 | 5 | //////////////////////////////////////////// 6 | // Generics Implementation by lil5 // 7 | // Visit him at: https://github.com/lil5 // 8 | // Thank You! // 9 | //////////////////////////////////////////// 10 | 11 | // Generics Require Go v1.18+ 12 | 13 | // Generic T 14 | // 15 | // The 'T' Type is the type of variables that will be going inside the queue slice 16 | // The Generic T is can be declared as any so it is possible to have multiple types 17 | // within the Queue Slice 18 | type Item[T any] any 19 | 20 | // The 'ItemQueue' Struct contains the []T slice 21 | // This struct holds two keys, 22 | // - items -> the []T slice 23 | // - mutex -> the mutex lock which prevents overwrites and data corruption 24 | // ↳ We use RWMutex instead of Mutex as it's better for majority read slices 25 | type ItemQueue[T any] struct { 26 | items []T 27 | mutex *sync.RWMutex 28 | } 29 | 30 | // Create() -> *ItemQueue 31 | // The Create() function will return an empty ItemQueue 32 | func Create[T any]() *ItemQueue[T] { 33 | return &ItemQueue[T]{ 34 | mutex: &sync.RWMutex{}, 35 | items: []T{}, 36 | } 37 | } 38 | 39 | // q.Index(index integer) -> *Item 40 | // The RemoveAtIndex() function is used to remove an item at the provided index of the ItemQueue 41 | // The function will then return the removed item if the user requires it's use 42 | func (q *ItemQueue[T]) RemoveAtIndex(i int) T { 43 | // Lock the queue then unlock once function closes 44 | q.mutex.Lock() 45 | defer q.mutex.Unlock() 46 | 47 | // Create a copy of the item 48 | var copy T = q.items[i] 49 | 50 | // Remove the item at the index 51 | q.items = append(q.items[:i], q.items[i+1:]...) 52 | 53 | // Returen the copy of the item 54 | return copy 55 | } 56 | 57 | // q.Contains(Item) -> None 58 | // The Contains() function will scheck whether the provided ItemQueue contains 59 | // 60 | // the given Item (_item) 61 | func (q *ItemQueue[T]) Contains(item any) bool { 62 | // Lock Reading 63 | q.mutex.RLock() 64 | defer q.mutex.RUnlock() 65 | 66 | // Iterate over the queue 67 | for i := 0; i < len(q.items); i++ { 68 | if any(q.items[i]) == item { 69 | return true 70 | } 71 | } 72 | return false 73 | } 74 | 75 | // q.Remove(Item) -> None 76 | // The Remove() function will secure the ItemQueue before iterating 77 | // 78 | // through said ItemQueue and remove the given Item (_item) 79 | func (q *ItemQueue[T]) Remove(item any) { 80 | // Lock the queue then unlock once function closes 81 | q.mutex.Lock() 82 | defer q.mutex.Unlock() 83 | 84 | // Iterate ove the queue items 85 | for i := 0; i < len(q.items); i++ { 86 | if any(q.items[i]) == item { 87 | q.items = append(q.items[:i], q.items[i+1:]...) 88 | return 89 | } 90 | } 91 | } 92 | 93 | // q.Put(Item) -> None 94 | // The Put() function is used to add a new item to the provided ItemQueue 95 | func (q *ItemQueue[T]) Put(i T) { 96 | // Lock the queue then unlock once function closes 97 | q.mutex.Lock() 98 | defer q.mutex.Unlock() 99 | 100 | // Append the new item to the item queue 101 | q.items = append(q.items, i) 102 | } 103 | 104 | // q.Get() -> Item 105 | // The Get() function will append the first item of the ItemQueue to the back of the slice 106 | // 107 | // then remove it from the front 108 | // 109 | // The function returns the first item of the ItemQueue 110 | func (q *ItemQueue[T]) Get() T { 111 | // Lock the queue then unlock once function closes 112 | q.mutex.Lock() 113 | defer q.mutex.Unlock() 114 | 115 | // Create a copy of the item 116 | var copy T = q.items[0] 117 | 118 | // Move the item at the front to the back 119 | q.items = append(q.items, q.items[0]) 120 | q.items = q.items[1:] 121 | 122 | // Return the copy 123 | return copy 124 | } 125 | 126 | // q.Grab() -> Item 127 | // The Grab() function will return the first item of the ItemQueue then 128 | // 129 | // remove it from said ItemQueue 130 | func (q *ItemQueue[T]) Grab() T { 131 | // Lock the queue then unlock once function closes 132 | q.mutex.Lock() 133 | defer q.mutex.Unlock() 134 | 135 | // Create a copy of the item 136 | var copy T = q.items[0] 137 | 138 | // Remove it from the queue 139 | q.items = q.items[1:] 140 | 141 | // Return the copy 142 | return copy 143 | } 144 | 145 | // q.Clear() -> None 146 | // The Clear() function will secure the queue then remove all of its items 147 | func (q *ItemQueue[T]) Clear() { 148 | // Lock the queue then unlock once function closes 149 | q.mutex.Lock() 150 | defer q.mutex.Unlock() 151 | 152 | // Set the queue items to empty 153 | q.items = []T{} 154 | } 155 | 156 | // q.Show() -> *[]Item 157 | // The Show() function will return the ItemQueue's items 158 | func (q *ItemQueue[T]) Show() []T { 159 | // Lock Reading 160 | q.mutex.RLock() 161 | defer q.mutex.RUnlock() 162 | 163 | // Create a copy of the items 164 | var copy []T = q.items 165 | 166 | // Return the copy 167 | return copy 168 | } 169 | 170 | // q.GetAtIndex(index integer) -> *Item 171 | // The GetAtIndex() function is used to return an item at the provided index of the ItemQueue 172 | func (q *ItemQueue[T]) GetAtIndex(i int) T { 173 | // Lock Reading 174 | q.mutex.RLock() 175 | defer q.mutex.RUnlock() 176 | 177 | // Create a copy of the item at the index 178 | var copy T = q.items[i] 179 | 180 | // Return the copy 181 | return copy 182 | } 183 | 184 | // q.IsEmpty() -> bool 185 | // The IsEmpty() function will return whether the provided ItemQueue contains any Items 186 | func (q *ItemQueue[T]) IsEmpty() bool { 187 | // Lock Reading 188 | q.mutex.RLock() 189 | defer q.mutex.RUnlock() 190 | 191 | // Return whether queue is empty 192 | return len(q.items) == 0 193 | } 194 | 195 | // q.IsNotEmpty() -> bool 196 | // The IsNotEmpty() function will return whether the provided ItemQueue contains any Items 197 | func (q *ItemQueue[T]) IsNotEmpty() bool { 198 | 199 | // Lock Reading 200 | q.mutex.RLock() 201 | defer q.mutex.RUnlock() 202 | 203 | // Return whether length is greater than 0 204 | return len(q.items) > 0 205 | } 206 | 207 | // q.Size() -> int 208 | // The Size() function will return the length of the ItemQueue slice 209 | func (q *ItemQueue[T]) Size() int { 210 | 211 | // Lock Reading 212 | q.mutex.RLock() 213 | defer q.mutex.RUnlock() 214 | 215 | // Return the queue length 216 | return len(q.items) 217 | } 218 | --------------------------------------------------------------------------------