Stage 1 · Code
Your First Real Program
JSON Storage
Serialize and deserialize your task data with encoding/json — including field tags, pretty printing, and error handling.
Marshal and Unmarshal
json.Marshal encodes a Go value to a JSON byte slice. json.MarshalIndent adds human-readable indentation. json.Unmarshal decodes a JSON byte slice back into a Go value.
Streaming with Decoder
json.NewDecoder reads from any io.Reader — a file, an HTTP response body, or stdin. It is more efficient than reading the entire content into memory first when working with large files.
Field Tags in Depth
Struct field tags are string literals that control how encoding packages handle each field. The json key is the most common, but the same struct can carry tags for multiple packages.
| Tag | Effect |
|---|---|
json:"name" | Use 'name' as the JSON key |
json:"name,omitempty" | Skip field if it is the zero value |
json:"-" | Never include this field in JSON |
json:",string" | Encode numeric type as a JSON string |
json:"name,omitempty,string" | Multiple options combined |
Handling Corrupt Data
JSON files can be corrupted by interrupted writes, manual edits, or encoding bugs. Always handle decode errors explicitly and provide a recovery path.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.