https://github.com/sashka/atomicfile
Atomic file writes in Go on Linux, macOS, and Windows.
https://github.com/sashka/atomicfile
atomic go golang
Last synced: 5 months ago
JSON representation
Atomic file writes in Go on Linux, macOS, and Windows.
- Host: GitHub
- URL: https://github.com/sashka/atomicfile
- Owner: sashka
- License: mit
- Created: 2020-05-15T17:17:15.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-25T22:03:08.000Z (about 6 years ago)
- Last Synced: 2024-06-20T01:50:59.430Z (almost 2 years ago)
- Topics: atomic, go, golang
- Language: Go
- Homepage:
- Size: 8.79 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# atomicfile
Package `atomicfile` provides `*os.File` wrapper to allow atomic file writes. Works on Linux, macOS, and Windows.
All writes will go to a temporary file.
Call `Close()` explicitly when you are done writing to atomically rename the file making the changes visible.
Call `Abort()` to discard all your writes.
This allows for a file to always be in a consistent state and never represent an in-progress write.
## Installation
Standard `go get`:
```
$ go get github.com/sashka/atomicfile
```
## Example
```
import "github.com/sashka/atomicfile"
// Prepare to write a file.
f, err := atomicfile.New(path, 0o666)
// It's safe to call f.Abort() on a successfully closed file.
// Otherwise it's correct to discard the file changes.
defer f.Abort()
// Update the file.
if _, err := f.Write(content); err != nil {
return err
}
// Make changes visible.
f.Close()
```