惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog RSS Feed
J
Java Code Geeks
C
Check Point Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Google DeepMind News
Google DeepMind News
阮一峰的网络日志
阮一峰的网络日志
Engineering at Meta
Engineering at Meta
Blog — PlanetScale
Blog — PlanetScale
D
Docker
H
Hackread – Cybersecurity News, Data Breaches, AI and More
月光博客
月光博客
I
InfoQ
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
A
About on SuperTechFans
L
LangChain Blog
腾讯CDC
Y
Y Combinator Blog
MongoDB | Blog
MongoDB | Blog
Vercel News
Vercel News
MyScale Blog
MyScale Blog
博客园 - Franky
IT之家
IT之家
博客园_首页

alexwlchan’s notes

What is WS11 1DB? Blocking referrers with Caddy How to type a Spanish question mark (¿) on a Mac Non-overlapping type comparisons and Python type checkers Why does t.Setenv panic after t.Parallel? Use Path.glob() and Path.rglob() for typed versions of glob.glob() Curious clocks and colourful eyes Track which templates are used by Jinja2 Archeologists distinguish between “sherds” and “shards” A single command to test all my changed Go packages Disable the new message animations in WhatsApp Finding high-churn folders that bother Backblaze Always-on SSH agent forwarding with my Git pushes Managing the caption of a photo with AppleScript (but not PhotoKit) Goodhart’s and Campbell’s Law are different Notes from The Cornishman No. 176 (Spring 2026) Notes from The Cornishman No. 176 (Spring 2026) GitUp can’t diff text files larger than 8MB Home Testing the width of a page on a mobile device using Playwright Disable AirPods charging notifications Start a Caddy server in a subprocess during a Python session Filter a list of JSON object based on a list of tags HOME_GET_ME_HOME is a Citymapper Shortcuts action The FileExistsError exception exposes a filename attribute The red-lined bubble snail Why can’t Python connect to example.com? Useful type hints for Python How to truncate the middle of long command output AirPlay Receiver can interfere with Flask apps
Create a file atomically in Go
2026-02-22 · via alexwlchan’s notes

Use os.CreateTemp to create a temporary file in the target directory, then do an atomic rename once you’ve finished writing.

Here’s an interesting function from the Tailscale repos that Anton told me about in a code review last week: a function to write to a file atomically. This ensures you don’t get partially written data in the final file.

10import (

11 "fmt"

12 "os"

13 "path/filepath"

14 "runtime"

15)

16

17// WriteFile writes data to filename+some suffix, then renames it into filename.

18// The perm argument is ignored on Windows, but if the target filename already

19// exists then the target file's attributes and ACLs are preserved. If the target

20// filename already exists but is not a regular file, WriteFile returns an error.

21func WriteFile(filename string, data []byte, perm os.FileMode) (err error) {

22 fi, err := os.Stat(filename)

23 if err == nil && !fi.Mode().IsRegular() {

24 return fmt.Errorf("%s already exists and is not a regular file", filename)

25 }

26 f, err := os.CreateTemp(filepath.Dir(filename), filepath.Base(filename)+".tmp")

27 if err != nil {

28 return err

29 }

30 tmpName := f.Name()

31 defer func() {

32 if err != nil {

33 f.Close()

34 os.Remove(tmpName)

35 }

36 }()

37 if _, err := f.Write(data); err != nil {

38 return err

39 }

40 if runtime.GOOS != "windows" {

41 if err := f.Chmod(perm); err != nil {

42 return err

43 }

44 }

45 if err := f.Sync(); err != nil {

46 return err

47 }

48 if err := f.Close(); err != nil {

49 return err

50 }

51 return Rename(tmpName, filename)

52}

Lines 10–52 of atomicfile/atomicfile.go in the tailscale/tailscale repo. Copyright Tailscale Inc & contributors, used under the BSD-3-Clause license.

This is similar to code I’ve produced in other projects to do atomic file writes – write to a temporary file first, then do an atomic rename to the final destination.

The temporary file is created in the same directory as the target, to give the best chance of being able to do an atomic rename. You can’t do an atomic rename across filesystem boundaries; using the same directory ensures both files are on the same filesystem.

To handle concurrent writes, I normally insert a random UUID into the temporary filename, so different processes write to different tempfiles. This is handled automatically by Go’s os.CreateTemp function, which adds a random string to the end of the filename.

The Rename() function has different logic for Windows and non-Windows systems:

  • On non-Windows, it uses os.Rename(). The Go documentation notes that “even within the same directory, on non-Unix platforms Rename is not an atomic operation”.
  • On Windows, it makes a syscall to the ReplaceFileW function. A cursory Internet search is conflicted on whether this is a truly atomic rename, although concurs that it’s the best option on Windows.