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
