Chapter 4.  Command-Line Tools to Find Domain Names

The chat application we've built so far is ready to take the world by storm but not before we give it a home on the Internet. Before we invite our friends to join the conversation, we need to pick a valid, catchy, and available domain name, which we can point to the server running our Go code. Instead of sitting in front of our favorite domain name provider for hours on end trying different names, we are going to develop a few command-line tools that will help us find the right one. As we do so, we will see how the Go standard library allows us to interface with the terminal and other executing applications; we'll also explore some patterns and practices to build command-line programs.

In this chapter, you will learn:

  • How to build complete command-line applications with as little as a single code file
  • How to ensure that the tools we build can be composed with other tools using standard streams
  • How to interact with a simple third-party JSON RESTful API
  • How to utilize the standard in and out pipes in Go code
  • How to read from a streaming source, one line at a time
  • How to build a WHOIS client to look up domain information
  • How to store and use sensitive or deployment-specific information in environment variables

Pipe design for command-line tools

We are going to build a series of command-line tools that use the standard streams (stdin and stdout) to communicate with the user and with other tools. Each tool will take an input line by line via the standard input pipe, process it in some way, and then print the output line by line to the standard out pipe for the next tool or user.

By default, the standard input is connected to the user's keyboard, and the standard output is printed to the terminal from where the command was run; however, both can be redirected using redirection metacharacters. It's possible to throw the output away by redirecting it to NUL on Windows or /dev/null on Unix machines, or redirecting it to a file that will cause the output to be saved to a disk. Alternatively, you can pipe (using the | pipe character) the output of one program to the input of another; it is this feature that we will make use of in order to connect our various tools together. For example, you could pipe the output from one program to the input of another program in a terminal using this code:

echo -n "Hello" | md5

The output of the echo command will be the string Hello (without the quotes), which is then piped to the md5 command; this command will in turn calculate the MD5 hash of Hello:

8b1a9953c4611296a827abf8c47804d7

Our tools will work with lines of strings where each line (separated by a linefeed character) represents one string. When run without any pipe redirection, we will be able to interact directly with the programs using the default in and out, which will be useful when testing and debugging our code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.107.100