Files and JSON in Go
Files and JSON is where your programs learn to save and share data.
In this chapter, you will:
- create, read, write and append files
- work with folders, file details and file errors
- turn Go values into JSON and back
- use JSON for settings, saved data and APIs
What each lesson covers:
- File Handling: Create, read, write and append files with os and bufio, make and list directories, read file info and permissions, and handle missing-file and permission errors.
- JSON: JSON in Go with encoding/json: JSON structure, json.Marshal and MarshalIndent, json.Unmarshal, struct tags, nested JSON, and uses like config files and API responses.
This chapter has 2 lessons. Plan on about 50 minutes, with 6 exercises to check what you learned. Every example is real Go code: change it, run it again and compare the output, which is the fastest way to learn how each feature behaves.
Simple lesson: finish this chapter and your programs can remember data between runs.
Lessons in this section
- File Handling
Create, read, write and append files with os and bufio, make and list directories, read file info and permissions, and handle missing-file and permission errors.
- Explain why file handling matters
- Create, read, write and append files
- Create and read directories
- Read file metadata and permissions
- JSON
JSON in Go with encoding/json: JSON structure, json.Marshal and MarshalIndent, json.Unmarshal, struct tags, nested JSON, and uses like config files and API responses.
- Describe JSON objects and arrays
- Encode Go values with Marshal and MarshalIndent
- Decode JSON with Unmarshal
- Use struct tags and nested JSON