2 A solid foundation: Building a command-line application

 

This chapter covers

  • Working with command-line flags, options, and arguments
  • Passing configuration into an application
  • Starting and gracefully stopping a web server
  • Path routing for web and API servers

This chapter covers several foundational areas for developing command-line interface (CLI) applications. You’ll learn about handling command-line options—sometimes called flags or getopts —in a way that’s consistent with modern applications for Linux and other Portable Operating System Interface (POSIX) systems.

We’ll follow up by looking at several ways to pass configuration into an application, including environment variables and various popular file formats used to store configuration. You’ll also learn a bit about structs and interfaces; you’ll see how to couple them tightly to maintain state for configuration. You’ll look at Go’s enum support and drawbacks as part of this topic.

From there, you’ll move on to building and starting a simple server from the command line and learning best practices for starting and stopping it. Last, you’ll learn URL path-matching techniques for websites and servers, providing a Representational State Transfer (REST) API, which will give your server a bit more functionality.

2.1 Building CLI applications the Go way

2.1.1 Command-line flags

2.1.2 Defining valid values via enums

2.1.3 Slices, arrays, and maps

2.1.4 Command-line frameworks

2.2 Handling configuration

2.2.1 Using configuration files

2.2.2 Configuration via environment variables

2.3 Working with real-world web servers

2.3.1 Starting up and shutting down a server

2.3.2 Graceful shutdowns using OS signals

2.3.3 Routing web requests

Summary