LatchfileDocumentation

API reference

Both write functions create a temporary sibling of path. On success they replace the destination with the completed file.

WriteFile

func WriteFile(path string, data []byte, mode fs.FileMode) error

Writes the whole byte slice. Relative paths resolve against the current working directory. The function returns errors from creation, writing, or replacement.

Write

func Write(path string, mode fs.FileMode, write func(io.Writer) error) error

Hands the temporary file's writer to a callback. Return any encoder or flush error. Finish all writes before the callback returns. The package owns the file descriptor and closes it before the rename.

Errors

Use errors.Is for operating-system errors such as fs.ErrPermission and fs.ErrNotExist. Preserve the original error with %w when adding context.

See troubleshooting for common failures.