Skip to main content
Module 8 · Lesson 3 of 538/50 in the course~14 min
Module lessons (3/5)

`io` and `os`: files and streams

Lesson preview5 hands-on exercises readyCourse Go · Module 8 — Essential standard library

os.Open/Create/ReadFile/WriteFile, the io.Reader/Writer interfaces, defer f.Close() patterns.

What you'll learn in this lesson

  • Read and write entire files safely with os.ReadFile and os.WriteFile.
  • Open large files in streaming by always closing the resource with defer f.Close().
  • Validate command line arguments using os.Args.
  • Transfer data between any io.Reader and io.Writer with io.Copy and bufio.Scanner.

Overview

In Go, files, network connections, in-memory buffers, and even HTTP bodies share two minimal interfaces: io.Reader and io.Writer. Combining os to interact with the operating system and io to compose streams makes the code reusable and easy to test.

When a configuration file or payload is small (a few MB at most), you can read or write it in a single atomic call on the code:

Sections covered in this lesson

  1. 1Shortcuts for small files: ReadFile and WriteFile
  2. 2Controlled streaming for large files
  3. 3The io.Reader and io.Writer interfaces
  4. 4CLI arguments and io.EOF in manual loops
  5. 5Guided practice
  6. 6Check for understanding
Pro Lesson · Module 8 — Essential standard library

Unlock "`io` and `os`: files and streams" and the full Course Go curriculum

`os.Open/Create/ReadFile/WriteFile`, the `io.Reader/Writer` interfaces, `defer f.Close()` patterns.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in Course Go plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.