liveBook · Manning

Help message

As explained in Go by Example

A usage message is command-line help text describing a program's available options, types, defaults, and invocation syntax.

A help message describes how to use a command-line tool: its flags, argument types, defaults, required inputs, and invocation syntax. It guides users toward valid commands and reduces the need for separate error-handling text. section 4.1.1 section 4.3.2

A useful help message can communicate:

  • Available options such as URL, request count, concurrency, and requests per second. section 4.1.1
  • Flag types, sensible defaults, and required values generated from flag definitions. section 4.3.2
  • The distinction between optional flags and required positional arguments, such as `usage: hit [options] url`. section 4.5.3

For a first version, a program can store multiline help text in a raw string literal and print it with the logo. Once parsing moves to a `flag.FlagSet`, the standard library can generate usage output from registered flags and handle parsing errors. Assigning `FlagSet.Usage` enables a custom header and positional-argument description; write through `FlagSet.Output()` so help and errors use the same destination. section 4.1.1 section 4.5.2 section 4.3.2

A positional URL with optional flags

Running `hit -h` can produce `usage: hit [options] url`, followed by `-c`, `-n`, and `-rps` with descriptions and defaults. A command such as `hit -c 2 -n 100 https://github.com/inancgumus` combines optional flags with the required URL. section 4.5.3

Listing 4.1 Printing usage (hit/cmd/hit/hit.go)
package main

import "fmt"

const logo = `  #1
 __  __     __     ______
/\ \_\ \   /\ \   /\__  _\
\ \  __ \  \ \ \  \/_/\ \/
 \ \_\ \_\  \ \_\    \ \_\
  \/_/\/_/   \/_/     \/_/`  #1

const usage = `  #1
Usage:
  -url
       HTTP server URL (required)
  -n
       Number of requests
  -c
       Concurrency level
  -rps
       Requests per second` #1

func main() {
    fmt.Printf("%s\n%s", logo, usage)
}

View a complete runnable starting point that prints a logo and a multiline usage message. section 4.1.1

How Go by Example covers it

How deep it goes
Explains handwritten and FlagSet-generated help, then customizes output for positional arguments; it does not cover broader CLI documentation.
Examples
Uses the HIT tool, multiline raw strings, generated `-h` output, invalid flag input, and a positional HTTP server URL.
What it assumes
Assumes basic Go command-line programs and flags while teaching raw strings, FlagSet registration, parsing, and usage customization.
What you can do after
Build it from scratch, generate help with Go's flag package, or customize usage for required positional arguments.
How it connects
Connects command-line interfaces with flags, FlagSet, positional arguments, parsing errors, and standard error output.

Also explained in

Compare how 2 books explain help message →

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.