An educational implementation of a segmented log. Not suited for production.
Run
go get github.com/RaphSku/clogto fetch clog as a dependency.
Here is an example demonstrating how to use clog:
func convertStringToBytes(s string) []byte {
buf := new(bytes.Buffer)
buf.WriteString(s)
return buf.Bytes()
}
func main() {
logDir := "./logs"
l, err := clog.NewLog(logDir)
if err != nil {
fmt.Printf("failed to create a new log: %v\n", err)
os.Exit(1)
}
data := convertStringToBytes("Hello World!")
err = l.Write(data)
if err != nil {
fmt.Printf("failed to write to log: %v\n", err)
os.Exit(1)
}
offset := int64(0)
data, err = l.Read(offset)
if err != nil {
fmt.Printf("failed to read from log: %v\n", err)
os.Exit(1)
}
fmt.Printf("Fetched the following message at offset %d: %s\n", offset, string(data))
}