liveBook · Manning

Standard library

As explained in Go in Practice, Second Edition

A standard library is a collection of reusable modules, types, functions, APIs, and other facilities distributed with a programming language implementation or platform.

Go’s standard library supplies reusable packages for common application work, including networking, cryptography, serialization, and math. Its packages are imported through the same package system used for application code and external dependencies. The full library is documented with examples at pkg.go.dev/std. section 1.2.2 section 1.2.4

Start with the package that matches the task. For a direct TCP connection, net.Dial accepts a connection type and endpoint; net.Listen accepts incoming connections. For an ordinary web request, net/http offers the simpler http.Get. Check the returned error, close the response body, and read it with io.ReadAll. section 1.2.2

Listing 1.4 HTTP GET
package main

import (
    "io"
    "log"
    "net/http"
)

func main() {
    resp, err := http.Get("http://example.com/")       #1
    if err != nil {
        log.Fatal("could not retrieve example.com", err)    #2
    }
    defer resp.Body.Close()                 #3

    body, err := io.ReadAll(resp.Body)        #4
    if err != nil {
        log.Fatal("could not read body", err)   #5
    }
    log.Println(string(body))
}

This HTTP GET example combines net/http, io, and log to retrieve a page, handle failures, close the body, and print its contents. section 1.2.2

Import a package by name and prefix its exported functions with that name: fmt.Println uses fmt, while http.Get uses net/http under the http prefix. Imports can be grouped; external packages use the same import mechanism and can be retrieved with go get. Standard-library packages also support practical command-line programs: built-in functionality handles arguments and flags, while packages such as bufio, os, and strings support input processing. section 1.2.4 section 2.1

The library has boundaries. It does not include native GUI application support; that calls for other libraries or frameworks. A standard-library import is also distinct from a toolchain command: testing supplies test facilities, while go test runs tests and can report coverage. section 2.1 section 1.2.4

How Go in Practice, Second Edition covers it

How deep it goes
Works through imports and network-client calls, while pointing to package documentation rather than cataloging every API.
Examples
Uses a TCP status request, an HTTP GET, formatted output, input processing, and a test run.
What it assumes
Introduces package imports with fmt.Println before combining packages; does not require prior familiarity with Go’s package system.
What you can do after
Apply it via Go packages to make network requests, process command-line input, and use test facilities.
How it connects
Connects package imports, dependency management, HTTP, command-line flags, and testing.

Also explained in

Compare how 7 books explain standard library →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Practice, Second Edition. The section links point to the parts of the book it draws on.