Chapter 2 Interacting with Your Users

Powerful Command-Line Applications in Go — by Ricardo Gerardi (17 / 127)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Wrapping Up | TOC | Organizing Your Code 👉

Unlike with a graphical program, the user of a CLI tool generally provides up front all the input and parameters required for the tool to work. The tool uses that input to do its job and provides results back to the user as text output on the screen. When an error occurs, a CLI tool usually provides details about it in a way that’s easy and practical for the user to understand or potentially filter out.

In this chapter, you’ll get comfortable working with input and output as you build a command-line tool for managing a list of “to-do” items. This tool will let you keep track of items left in a project or activity. The tool will save the list of items in a file using the JSON format.

To implement this tool, you’ll accept input data from your users in a variety of ways. You’ll get input from standard input (STDIN) and command-line parameters. You’ll also use environment variables to modify how your program works. In addition, you’ll display information back to the user through standard output (STDOUT) and output errors with the standard error (STDERR) stream for proper CLI error handling. Finally, you’ll explore Go interfaces in general and the io.Reader interface in particular.

Let’s start with a basic implementation of this tool and improve it along the way.

👈 Wrapping Up | TOC | Organizing Your Code 👉

Powerful Command-Line Applications in Go by Ricardo Gerardi can be purchased in other book formats directly from the Pragmatic Programmers. If you notice a code error or formatting mistake, please let us know here so that we can fix it.

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.