Minimalist Append Only File API
go get github.com/jeroiraz/go-aofpackage main
import (
"log"
"math/rand"
"time"
"github.com/jeroiraz/go-aof"
)
func randomBytes(size int) []byte {
rand.Seed(time.Now().UnixNano())
b := make([]byte, size)
rand.Read(b)
return b
}
func main() {
app, err := aof.Open("file.aof")
if err != nil {
panic(err)
}
defer app.Close()
for i := 1; i <= 10; i++ {
b := randomBytes(i)
_, err := app.Append(b)
if err != nil {
panic(err)
}
}
err = app.ForEach(func(e *aof.Entry) (cutoff bool, err error) {
log.Printf("Entry at offset: %d has size: %d", e.Offset(), e.Size())
return false, nil
})
if err != nil {
panic(err)
}
mr, err := app.Map(func(e *aof.Entry) (size interface{}, cutoff bool, err error) {
return e.Size(), false, nil
})
if err != nil {
panic(err)
}
log.Printf("Sizes: %v", mr)
}