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.