liveBook · Manning

Standard library

As explained in Go by Example

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 is a rich collection of packages bundled with the language, covering work from string manipulation to HTTP servers. Reusing these packages speeds development and favors code that is tested, runtime-integrated, efficient, and familiar to other Go developers. section 1.6

Prefer standard-library packages when they meet the need because they provide a common foundation for practical programs:

  • Build cross-platform, efficient command-line tools with standard-library support. section 1.2
  • Create clients and servers with built-in HTTP and TCP networking support. section 1.2
  • Use packages such as testing for automated tests, flag for command-line flags, and http for requests and services. section 1.6
  • Add third-party packages through Go’s module system when everyday standard-library facilities do not cover specialized or advanced needs. section 1.6

A minimal HTTP server

The net/http package can listen on localhost:8080 and reply “hello, gophers!” to every request. The server serves requests concurrently with goroutines, showing how a small standard-library program can form a useful network service. section 1.6 section 8.3

package main

import "net/http"  #1

func main() {
    handler := func(  #2
        w http.ResponseWriter,  #3
        r *http.Request,  #4
    ) {
         w.Write([]byte("hello, gophers!"))  #5
    }
    http.ListenAndServe("localhost:8080", http.HandlerFunc(handler))
}

Inspect the complete server: a handler receives a ResponseWriter and Request, writes the response body, and ListenAndServe starts the server. section 1.6

For more control, an http.Handler implementation supplies ServeHTTP. The server calls it with an http.ResponseWriter and *http.Request; set headers and status before writing the response body. section 8.3

How Go by Example covers it

How deep it goes
Defines the package collection and works through a minimal HTTP server, deferring detailed HTTP service structure.
Examples
Uses a localhost:8080 HTTP server that returns “hello, gophers!” and concurrent request handling.
What it assumes
Assumes programming and general computer-science fundamentals, including networks and concurrent programming, while introducing Go package use.
What you can do after
Apply it via Go packages to build everyday tools and network services, choosing third-party packages for unmet specialized needs.
How it connects
Connects testing, command-line flags, HTTP, TCP, goroutines, modules, and third-party packages.

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 by Example. The section links point to the parts of the book it draws on.