liveBook · Manning

Chapter 10: The standard library

What you will learn in chapter 10 of Go in Action, Second Edition

The introduction presents Go’s standard library and previews a chapter-long JSON and XML utility built by combining its packages.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction presents Go’s standard library and previews a chapter-long JSON and XML utility built by combining its packages.

Key terms: standard library, community libraries, JSON, XML, validation and conversion utility, Reader, Writer, standard library packages

10.1 The fmt package

Introduces the fmt package as a way to print CLI output, format and retain strings, and read terminal input.

Key terms: fmt.Print, fmt.Println, fmt.Printf, formatting verbs, escape codes, Sprint family, Print family, standard output, fmt.Scan, fmt.Scanf

10.2 The flag package

This section shows how to use Go’s flag package to configure a command-line program and handle required positional arguments.

Key terms: flag package, flag.Bool, flag.String, flag.Int, flag.Parse(), help flag, flag.Args(), positional arguments, input file, output file

10.3 The io package

The section shows how the io package’s small interfaces and stream utilities make I/O code reusable across different sources and destinations.

Key terms: io.Reader, io.Writer, process function, fmt.Fprintf, fmt.Printf, io.Closer, io.ReadCloser, io.WriteCloser, io.ReaderWriter, io.ReadAll

10.4 The os package

Shows how the os package helps a portable CLI handle exit status, standard streams, files, temporary resources, and environment variables.

Key terms: os.Exit, exit code, deferred function calls, os.Stdin, os.Stdout, os.Stderr, io.Reader, io.Writer, os.File, os.Open

10.5 The encoding package

Introduces Go’s encoding packages as tools for converting structured data and composing those conversions with I/O, ahead of a JSON and XML tool.

Key terms: serialization, deserialization, BinaryMarshaler, TextMarshaler, encoding/json, encoding/xml, byte slices, io.Reader, io.Writer, data processing pipelines

10.5.1 JSON processing

This section shows how to map server configuration JSON to Go structs, convert values with encoding/json, and customize conversion for validation and redaction.

Key terms: encoding/json, struct field tags, exported fields, json.Unmarshal, json.Marshal, byte slice, Unmarshaler, UnmarshalJSON, temporary anonymous struct, Host

10.5.2 XML processing

The section shows how one Go struct can support both JSON and XML and serve as the intermediate representation for converting between them.

Key terms: Config, struct tags, encoding/xml, Marshal, Unmarshal, intermediate Go struct, bidirectional conversion, attributes, namespaces, mixed content

10.6 The bufio package

The section shows how bufio wraps Go’s I/O interfaces to make reading and writing more efficient, and how a JSON/XML converter can use buffered input and output.

Key terms: bufio, io.Reader, io.Writer, buffering, system calls, bufio.Reader, ReadString, ReadLine, Peek, bufio.Scanner

10.7 The regexp package

This section introduces Go’s regexp package for validating text, explains basic pattern syntax, and gives guidance on performance and maintainability.

Key terms: regular expression, metacharacters, character classes, anchors, greedy selectors, lazy, *?, +?, regexp package, *regexp.Regexp

10.8 The strings package

This section introduces the strings package as a set of composable functions for manipulating and examining strings, especially when processing input in CLI tools.

Key terms: strings package, substring, prefix, suffix, case-insensitive comparison, input validation, UTF-8 encoded strings, splitting, joining, replacing

10.9 The strconv package

This section shows how to use strconv to parse text into typed values and format values as strings with explicit error handling and control over output.

Key terms: strconv, parsing, typed values, error handling, ParseBool, Boolean, fmt.Sprintf, FormatFloat, numeric base, format

10.10 Scratching the surface

This section recaps the completed JSON/XML CLI tool and encourages readers to look for standard-library solutions before turning to community packages.

Key terms: flag, os, io, encoding/json, encoding/xml, regexp, Go standard library, pkg.go.dev/std, community package

10.11 Summary

The section recaps standard-library packages for command-line programs, I/O, structured-data encoding, and text processing.

Where this is used later

Concepts explained in this chapter

← Chapter 9: Concurrency Chapter 11: Working with larger projects →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.