Skip to Content
Hands-On Software Engineering with Golang
book

Hands-On Software Engineering with Golang

by Achilleas Anagnostopoulos
January 2020
Intermediate to advanced
640 pages
16h 56m
English
Packt Publishing
Content preview from Hands-On Software Engineering with Golang

Implementing an in-memory, thread-safe queue

For the majority of applications, using an in-memory queue implementation such as the one presented here should suffice. Implementing support for other types of queue systems (for example, Kafka, nats-streaming, or even plain files) is left as an exercise for you.

Let's start by defining the inMemoryQueue type and its constructor:

type inMemoryQueue struct {
    mu   sync.Mutex
    msgs []Message

    latchedMsg Message
}

func NewInMemoryQueue() Queue {
    return new(inMemoryQueue)
}

As you can see, the in-memory queue is nothing more than a slice of Message instances a slot for storing the message that's being currently pointed to by an iterator and a sync.Mutex for serializing access to the list of messages. ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Hands-On Software Architecture with Golang

Hands-On Software Architecture with Golang

Jyotiswarup Raiturkar

Publisher Resources

ISBN: 9781838554491Supplemental Content