Latchfile

Go package / latchfile

Atomic file writes for Go.

Save configuration, checkpoints, and generated files in a single step. Latchfile writes to a temporary file beside the destination and renames it into place when the write succeeds.

Read the docs

Documentation

Quickstart

Pass the destination, the bytes you want to save, and the file mode. Handle the returned error before continuing.

settings.go
import "example.org/latchfile"

data := []byte("{\"workers\":4}\n")
err := latchfile.WriteFile("settings.json", data, 0644)
if err != nil {
    return err
}

API reference

WriteFile(path, data, mode) error
Writes the supplied bytes and replaces the destination. The parent directory must already exist.
Write(path, mode, func(io.Writer) error) error
Streams data through a callback. Returning an error discards the temporary file.