Skip to Content
Practical Go
book

Practical Go

by Amit Saha
December 2021
Beginner to intermediate
416 pages
9h 36m
English
Wiley
Content preview from Practical Go

CHAPTER 2Advanced Command-Line Applications

In this chapter, you will learn how to use the flag package to implement command-line applications with sub-commands. Then, you will see how you can enforce predictable behavior in your command-line applications using contexts. Finally, you will learn how to combine contexts and handling operating system signals in your application. Let's jump in.

Implementing Sub-commands

Sub-commands are a way to split the functionality of your command-line application into logically independent commands having their own options and arguments. You have a top-level command—your application—and then you have a set of sub-commands, each having its own options and arguments. For example, the Go toolchain is distributed as a single application, go, which is the top-level command. As a Go developer, you will interact with its various functionalities via dedicated sub-commands such as build, fmt, and test .

You will recall from Chapter 1 that to create a command-line application, you first created a FlagSet object. For creating an application with sub-commands, you will create one FlagSet object per sub-command. Then, depending on which sub-command is specified, the corresponding FlagSet object is used to parse the remaining command-line arguments (see Figure 2.1).

Schematic illustration of the main application looks at the command-line arguments and invokes the appropriate sub-command handler if possible.

Figure 2.1: The main application looks at the command-line arguments and invokes the ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Mastering Go - Third Edition

Mastering Go - Third Edition

Mihalis Tsoukalos
Efficient Go

Efficient Go

Bartlomiej Plotka
GitOps Cookbook

GitOps Cookbook

Natale Vinto, Alex Soto Bueno

Publisher Resources

ISBN: 9781119773818Purchase Link