zserge / atomicwriter

Atomic file writes in Go (using a unique temporary file and atomic rename)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

atomicwriter

Atomic file writer for Go. It uses a temporary file for writing, and when close is performed - the temporary file is renamed back to the original file name.

Rename is supposed to be atomic (as it is on most platforms).

Example:

package main

import (
	"log"

	"github.com/zserge/atomicwriter"
)

func main() {
	f, err := atomicwriter.NewWriter("file.txt")
	if err != nil {
		log.Panic(err)
	}
	defer f.Close()

	f.Write([]byte("Hello"))
	f.Write([]byte("world"))
	f.Write([]byte("\n"))
}

About

Atomic file writes in Go (using a unique temporary file and atomic rename)

License:MIT License


Languages

Language:Go 94.9%Language:Shell 5.1%