Skip to content

Installation

  • Go 1.26 or later
  • An active Go module (go.mod)

Run this in your project directory:

Terminal window
go get github.com/larsartmann/go-atomic-write@latest
package main
import (
"errors"
"fmt"
"os"
atomicwrite "github.com/larsartmann/go-atomic-write"
)
func main() {
path := "/path/to/config.json"
data, err := os.ReadFile(path)
if err != nil && !os.IsNotExist(err) {
panic(err)
}
fp := atomicwrite.FingerprintFromBytes(data)
newData := []byte(`{"updated": true}`)
err = atomicwrite.Write(path, newData, fp)
if errors.Is(err, atomicwrite.ErrConcurrentModification) {
fmt.Println("file was modified concurrently — re-read and retry")
return
}
if err != nil {
panic(err)
}
}
Terminal window
go list -m github.com/larsartmann/go-atomic-write