github.com/antifuchs/o (go): Package o provides a ring-buffer accounting abstraction that allows you to build your own ring buffers without having to dynamically cast values from interface{} back and forth.  The trick to having a type-safe ring buffer is simple: You define a new structure that contains the accounting interface (defined here) and a buffer of the appropriate capacity on it. The accounting interface gives your code the indexes that it needs to update the ring at, and your code can go its merry way.  For an example, see the ring buffer backed ReadWriter defined in package ringio.  The ring buffer accountants defined in this package all return errors if they're full or empty. To simply overwrite the oldest element, use function ForcePush.  If your code needs to only inspect the contents of the ring instead of shifting them out, you can use the Inspect method (which returns ranges, see the next section) or a stateful iterator in either LIFO or FIFO direction available to do this conveniently:  See Scanner for defails and usage examples.  A Ring assumes that all indexes between the first occupied index (the "read" end) and the last occupied index (the "write" end) are continuously occupied. Since rings wrap around to zero, that doesn't mean however, that each continuous index is greater than the index before it  To make it easier to deal with these index ranges, every operation that deals with ranges (e.g. Inspect, Consume, PushN) will return two Range objects, by convention named first and second.  These ranges cover the two parts of the buffer. Assume a ring buffer like this, x marking occupied elements and _ marking unoccupied ones:  The way o.Ranges represents a range between the Read and the Write end is:  Note that the End index of a range is the first index greater than the one that's occupied. This allows using these Range ends as points in a slice expression without modification.  None of the data structures provided here are safe from data races. To use them in thread-safe ring buffer implementations, users must protect both the accounting operations and backing buffer writes with a Mutex.  The ring buffer accounting techniques in this package and were translated into go from a post on the blog of Juho Snellman, https://www.snellman.net/blog/archive/2016-12-13-ring-buffers/.
Keywords: data-structures|golang|ring-buffer