LatchfileDocumentation

Recipes

Choose a recipe by how the data is produced and consumed. If the bytes are already in memory, WriteFile is enough. Use Write when an encoder can stream.

Stream JSON into a checkpoint

err := latchfile.Write("checkpoint.json", 0600, func(w io.Writer) error {
    enc := json.NewEncoder(w)
    enc.SetIndent("", "  ")
    return enc.Encode(snapshot)
})

An encoding error discards the temporary file. The last complete checkpoint stays available.

Export a CSV report

Call Flush on the CSV writer and return w.Error() before leaving the callback. A successful WriteAll already includes this flush. Buffered writers hide some errors until the final block is sent.

Publish several related files

Write each generation into its own directory and atomically replace a small manifest containing the active generation name. Readers open the manifest once and read everything from that generation. Delay cleanup until readers have released the old generation.