os.WriteFile
The Go function that writes a slice of bytes to a file in one call, creating it with the permission mode you give if it does not exist.
In more detail
The mode, such as 0o600, applies only when the file is created, and the process umask can remove bits from it. It replaces any existing contents. Choose a restrictive mode for anything sensitive.
Example
err := os.WriteFile("probe.conf", conf, 0o600)Related terms
- os.ReadFile — The Go function that reads a whole file into memory in one call and returns its bytes and an error.
- file permissions — Rules on each file saying which user and group own it and who may read, write or run it.
- octal — Writing numbers in base 8, using digits 0 to 7, which is the usual way to write Unix file permissions such as 600 or 755.
- umask — The builtin, and the per-process setting it controls, that lists which permission bits are removed from every new file and directory.
Taught in
- Files and the os Package — Concurrent Systems Programming with Go