Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const (
QueueErrorCodeIndexOutOfBounds = "index-out-of-bounds"
QueueErrorCodeFullCapacity = "full-capacity"
QueueErrorCodeInternalChannelClosed = "internal-channel-closed"
QueueErrorCodeValueNotFound = "value-not-found"
)

type QueueError struct {
Expand Down
19 changes: 19 additions & 0 deletions fifo_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}