diff --git a/error.go b/error.go index b8ef228..f9b93a9 100644 --- a/error.go +++ b/error.go @@ -6,6 +6,7 @@ const ( QueueErrorCodeIndexOutOfBounds = "index-out-of-bounds" QueueErrorCodeFullCapacity = "full-capacity" QueueErrorCodeInternalChannelClosed = "internal-channel-closed" + QueueErrorCodeValueNotFound = "value-not-found" ) type QueueError struct { diff --git a/fifo_queue.go b/fifo_queue.go index b3639f2..bf5c839 100644 --- a/fifo_queue.go +++ b/fifo_queue.go @@ -259,3 +259,22 @@ func (st *FIFO) IsLocked() bool { return st.isLocked } + +func (st *FIFO) RemoveByValue(value interface{}) error { + if st.isLocked { + return NewQueueError(QueueErrorCodeLockedQueue, "The queue is locked") + } + + st.rwmutex.Lock() + defer st.rwmutex.Unlock() + + // Find the first occurrence of the value + for i, item := range st.slice { + if item == value { + st.slice = append(st.slice[:i], st.slice[i+1:]...) + return nil + } + } + + return NewQueueError(QueueErrorCodeValueNotFound, fmt.Sprintf("value not found in queue: %v", value)) +}