Writing Files and Temp Files
WriteFile, OpenFile flags, and temp files
Writing Files and Temp Files is a free Go Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
os.WriteFile
os.WriteFile writes a byte slice to a file atomically (truncate then write). Simple for small files.
err := os.WriteFile("output.txt", []byte("hello"), 0644)
if err != nil { log.Fatal(err) }os.Create and defer Close
os.Create creates or truncates a file and returns an *os.File. Always close with defer.
f, err := os.Create("output.txt")
if err != nil { return err }
defer f.Close()
f.WriteString("hello\n")os.OpenFile with flags
Use os.OpenFile for append, create-if-not-exists, or exclusive creation modes:
f, err := os.OpenFile("log.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)bufio.Writer
Wrap a file in bufio.NewWriter to batch small writes into larger syscalls. Call Flush() before closing to write any buffered data.
w := bufio.NewWriter(f)
fmt.Fprintf(w, "line %d\n", i)
w.Flush()fmt.Fprintf
Write formatted text to any io.Writer including files:
fmt.Fprintf(f, "name: %s, age: %d\n", name, age)Atomic file write pattern
Write to a temp file, then rename to the destination. Rename is atomic on POSIX; readers see either the old or new file, never a partial write.
tmp, _ := os.CreateTemp("", "output-*.txt")
tmp.Write(data)
tmp.Close()
os.Rename(tmp.Name(), "output.txt")os.CreateTemp
os.CreateTemp(dir, pattern) creates a temp file in dir with a unique name matching pattern. Returns the open file. Caller is responsible for deletion.
f, err := os.CreateTemp("", "upload-*.json")
defer os.Remove(f.Name()) // cleanupos.MkdirTemp
os.MkdirTemp creates a temporary directory. Use it for test fixtures or processing pipelines that create multiple temp files.
dir, err := os.MkdirTemp("", "batch-*")
defer os.RemoveAll(dir)File permissions
Pass Unix permission bits (e.g., 0644 for owner rw, group r, other r) as the last argument to Create-style functions. The process umask may further restrict them.
Checking write errors
f.WriteString and fmt.Fprintf can return errors (disk full, permissions). Check errors or accumulate them with a writer wrapper that records the first error.
sync.File is not concurrent-safe
Multiple goroutines writing to the same *os.File concurrently may interleave writes. Use a mutex or a single dedicated writer goroutine.
Quick Check
Why is the write-to-temp-then-rename pattern considered atomic?
Recap: Writing Files
Key points:
- os.WriteFile: simple overwrite; bufio.Writer: batch small writes
- Atomic write: temp file + rename
- os.CreateTemp / os.MkdirTemp for temp resources; always defer cleanup
- Check write errors; synchronise concurrent writes
Frequently asked questions
Is the “Writing Files and Temp Files” lesson free?
Yes — the full text of “Writing Files and Temp Files” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “Writing Files and Temp Files”?
WriteFile, OpenFile flags, and temp files You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Writing Files and Temp Files” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Reading Files with os and bufio
- Writing Files and Temp Files
- JSON Encoding and Decoding
- Streaming JSON and CSV