Help us shape the future of Go! Take the 2026 Go Developer Survey

atomicfile

package
v1.0.65 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 20, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package atomicfile 提供不依赖命令层的安全原子文件写入。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Write

func Write(path string, data []byte, perm os.FileMode) error

Write 将 data 通过同目录临时文件、fsync 和 rename 原子写入 path。

func WriteJSON

func WriteJSON(path string, data []byte) error

WriteJSON 使用 0600 权限原子写入 JSON 数据。

func WriteWithOps

func WriteWithOps(path string, perm os.FileMode, ops Ops, writeFn func(TempFile) error) error

WriteWithOps 执行原子写入;Ops 仅供兼容测试注入和受控封装使用。

Types

type Ops

type Ops struct {
	MkdirAll   func(string, os.FileMode) error
	CreateTemp func(string, string) (TempFile, error)
	Remove     func(string) error
	Rename     func(string, string) error
}

Ops 允许上层保留已有的故障注入测试,同时把写入算法集中在本包。

type TempFile

type TempFile interface {
	io.Writer
	Name() string
	Chmod(os.FileMode) error
	Sync() error
	Close() error
}

TempFile 是原子写入所需的最小临时文件接口。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL