liveBook · Manning

Go doc

As explained in Go in Action, Second Edition

go doc is the standard Go command that extracts and presents documentation from Go source code.

Before writing a package call, check what it accepts and how it behaves. `go doc` reads documentation comments from Go source and prints package, function, and type documentation without a separate documentation tool. section 2.5.2 section 1.6

Use the command at the level of detail you need:

  • Run `go doc -all strings` for a detailed package listing, including descriptions and methods. Without `-all`, package output lists functions and types. section 2.5.2
  • Pass a package and type name to inspect the type definition, documentation, relevant constructor-like functions, and methods. section 2.8.1
  • Run `go doc -ex bufio.NewScanner` to see an example of constructing and using a scanner. section 2.8.1

Check the APIs for a word counter

The documentation for `strings.Fields` confirms that it splits on consecutive whitespace and returns an empty slice for whitespace-only input. A `bufio.Scanner` example shows a loop that reads tokens, uses `Text`, and checks `Err` after scanning. Together, those details support counting `len(strings.Fields(scanner.Text()))` for each scanned line. section 2.5.2 section 2.8.1

Listing 2.38 Example usage from go doc -ex bufio.NewScanner
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
	fmt.Println(scanner.Text())
}
if err := scanner.Err(); err != nil {
	fmt.Fprintln(os.Stderr, "reading standard input:", err)
}

The runnable-shaped scanner example shows the order of construction, scanning, reading text, and checking for errors. section 2.8.1

How Go in Action, Second Edition covers it

How deep it goes
Demonstrates package listings, detailed type output, and example lookup, but does not teach documentation-comment authoring.
Examples
Uses `strings.Fields` documentation and `bufio.Scanner` output to inform line-by-line word counting.
What it assumes
Assumes familiarity with Go packages and types while showing the documentation commands directly.
What you can do after
Apply it via `go doc` to inspect package APIs, type usage, and available examples before implementing a call.
How it connects
Connects documentation comments with Go packages, standard-library functions, types, methods, and constructor-like functions.

Related concepts

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