create a ring buffer api in go

A ring buffer, also known as a circular buffer, is a data structure that allows for constant time (O(1)) insertion and deletion of elements from both the beginning and the end of the buffer. In Go, we can implement a ring buffer using a slice with two pointers: one for the head, one for the tail of the buffer.

Here's an example implementation of a ring buffer API in Go:

main.go
package main

import (
    "errors"
)

type RingBuffer struct {
    buffer []interface{}
    head   int
    tail   int
    size   int
}

func NewRingBuffer(size int) *RingBuffer {
    return &RingBuffer{
        buffer: make([]interface{}, size),
        head:   0,
        tail:   0,
        size:   size,
    }
}

func (r *RingBuffer) Enqueue(item interface{}) error {
    if r.IsFull() {
        return errors.New("buffer is full")
    }
    r.buffer[r.tail] = item
    r.tail = (r.tail + 1) % r.size
    return nil
}

func (r *RingBuffer) Dequeue() (interface{}, error) {
    if r.IsEmpty() {
        return nil, errors.New("buffer is empty")
    }
    item := r.buffer[r.head]
    r.buffer[r.head] = nil
    r.head = (r.head + 1) % r.size
    return item, nil
}

func (r *RingBuffer) IsEmpty() bool {
    return r.head == r.tail
}

func (r *RingBuffer) IsFull() bool {
    return (r.tail+1)%r.size == r.head
}
915 chars
49 lines

Usage:

main.go
func main() {
    buffer := NewRingBuffer(5)
    buffer.Enqueue("a")
    buffer.Enqueue("b")
    buffer.Enqueue("c")
    buffer.Enqueue("d")
    buffer.Enqueue("e")
    // buffer.Enqueue("f") // returns "buffer is full" error
    item, _ := buffer.Dequeue() // item = "a"
    item, _ = buffer.Dequeue()  // item = "b"
    buffer.Enqueue("f")
    // buffer.Enqueue("g") // returns "buffer is full" error
}
405 chars
14 lines

In this implementation, head points to the front of the buffer, and tail points to the next available slot to insert a new element. The Enqueue method inserts an element at the next available slot and increments tail modulo buffer size. The Dequeue method removes and returns the element at the front of the buffer and increments head modulo buffer size. The IsEmpty and IsFull methods check if the buffer is empty or full respectively.

related categories

gistlibby LogSnag