Module lessons (3/5)
`io` and `os`: files and streams
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.ReadFileandos.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.Readerandio.Writerwithio.Copyandbufio.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
- 1Shortcuts for small files:
ReadFileandWriteFile - 2Controlled streaming for large files
- 3The
io.Readerandio.Writerinterfaces - 4CLI arguments and
io.EOFin manual loops - 5Guided practice
- 6Check for understanding
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.