go-buffer
represents a buffer that asynchronously flushes its contents. It is useful for applications that need to aggregate data before writing it to an external storage. A buffer is flushed manually, or automatically when it becomes full or after an interval has elapsed, whichever comes first.
go get github.com/omniboost/go-buffer
package main
import (
"time"
"github.com/omniboost/go-buffer"
)
func main() {
buff, err := buffer.New[any]().
// buffer can hold up to 5 items
buffer.WithSize(5).
// call this function when the buffer needs flushing
buffer.WithFlusher(buffer.FlusherFunc(func(items []interface{}) {
for _, item := range items {
println(item.(string))
}
})).
// Consume
Consume()
if err != nil {
panic(err)
}
// ensure the buffer
defer buff.Close()
buff.Push("item 1")
buff.Push("item 2")
buff.Push("item 3")
buff.Push("item 4")
buff.Push("item 5")
// block the current goroutine
time.Sleep(3 * time.Second)
println("done")
}
package main
import (
"time"
"github.com/omniboost/go-buffer"
)
func main() {
buff, err := buffer.New[any]().
// buffer can hold up to 5 items
buffer.WithSize(5).
// buffer will be flushed every second, regardless of
// how many items were pushed
buffer.WithFlushInterval(time.Second).
// call this function when the buffer needs flushing
buffer.WithFlusher(buffer.FlusherFunc(func(items []interface{}) {
for _, item := range items {
println(item.(string))
}
})).
// Consume
Consume()
if err != nil {
panic(err)
}
defer buff.Close()
buff.Push("item 1")
buff.Push("item 2")
buff.Push("item 3")
// block the current goroutine
time.Sleep(3 * time.Second)
println("done")
}
package main
import (
"time"
"github.com/omniboost/go-buffer"
)
func main() {
buff := buffer.New[any]().
// buffer can hold up to 5 items
buffer.WithSize(5).
// call this function when the buffer needs flushing
buffer.WithFlusher(buffer.FlusherFunc(func(items []interface{}) {
for _, item := range items {
println(item.(string))
}
})).
// Consume
Consume()
if err != nil {
panic(err)
}
defer buff.Close()
buff.Push("item 1")
buff.Push("item 2")
buff.Push("item 3")
// block the current goroutine
time.Sleep(3*time.Second)
buff.Flush()
println("done")
}
Visit Pkg.go.dev for full documentation.