small medium large xlarge

Powerful Command-Line Applications in Go: Build Fast and Maintainable Tools


Cover image for Powerful Command-Line Applications in Go

Powerful Command-Line Applications in Go

Build Fast and Maintainable Tools


Write your own fast, reliable, and cross-platform command-line tools with the Go programming language. Go might be the fastest—and perhaps the most fun—way to automate tasks, analyze data, parse logs, talk to network services, or address other systems requirements. Create all kinds of command-line tools that work with files, connect to services, and manage external processes, all while using tests and benchmarks to ensure your programs are fast and correct.

Choose Your Format(s)

  • $24.95 In Stock
    • Beta: What do I get?

  • Ebooks are DRM free.

  • Ebook delivery options.

About this Title

Pages: 250 (est)
Published: 2020-08-10
Release: B2.0 (2020-02-14)
ISBN: 978-1-68050-696-9

Go is a modern programming language that combines the reliability of compiled languages with the ease of use and flexibility of dynamic typed languages. With Go, you can develop cross-platform command-line tools that are fast and reliable. Work through practical examples to develop elegant and efficient tools by applying Go’s rich standard library, its built-in support for concurrency, and its expressive syntax. Use Go’s integrated testing capabilities to automatically test your tools, ensuring they work reliably even across code refactoring.

Develop CLI tools that interact with your users by using common input/output patterns, including environment variables and flags. Handle files to read or persist data, and manipulate paths consistently in cross-platform scenarios. Control processes and handle signals, and use a benchmark driven approach and Go’s concurrency primitives to create tools that perform well. Use powerful external libraries such as Cobra to create modern and flexible tools that handle subcommands, and develop tools that interact with databases, APIs, and network services. Finally, leverage what you learned by tackling additional challenges at the end of each chapter.

What You Need

Go 1.8 or higher, an internet connection to download the example files and additional libraries, and a text editor to write your programs.

Contents & Extracts

This book is currently in beta, so the contents and extracts will change as the book is developed.


  • What’s in This Book
  • How to Use This Book
  • About Example Code
  • Go Modules
  • Online Resources
  • Your First Command Line Program in Go excerpt
    • Building the Basic Word Counter
    • Testing the Basic Word Counter
    • Adding Command Line Flags
    • Compiling your Tool for Different Platforms
    • Exercises
    • Wrapping Up
  • Interacting with Your Users
    • Organizing your Code
    • Defining the To-Do API
    • Creating the Initial To-Do Command Line Tool
    • Testing the Initial CLI Implementation
    • Handling Multiple Command Line Options
    • Display Command Line Tool Usage
    • Improving the List Output Format
    • Increasing Flexibility with Environment Variables
    • Capturing Input from STDIN
    • Exercises
    • Wrapping Up
  • Working with Files in Go
    • Creating a Basic Markdown Preview Tool
    • Writing Tests for the Markdown Preview Tool
    • Adding Temporary Files to the Markdown Preview Tool
    • Using Interfaces to Automate Tests
    • Adding an Auto-Preview Feature
    • Cleaning Up Temporary Files
    • Improving the Markdown Preview Tool with Templates
    • Exercises
    • Wrapping Up
  • Navigating the File System
    • Developing a File System Crawler
    • Testing with Table Driven Testing
    • Deleting Matched Files
    • Testing with the Help of Test Helpers
    • Logging Deleted Files
    • Archiving Files
    • Exercises
    • Wrapping Up
  • Improving the Performance of your CLI Tools
    • Developing the Initial Version of colStats
    • Writing Tests for ColStats
    • Benchmarking your Tool
    • Profiling your Tool
    • Reducing Memory Allocation
    • Tracing your Tool
    • Improving the colStats Tool to Process Files Concurrently
    • Reduce Scheduling Contention
    • Exercises
    • Wrapping Up
  • Controlling Processes
    • Executing External Programs
    • Writing tests for Goci
    • Defining a Pipeline
    • Adding Another Step to the Pipeline
    • Handling Output from External Programs
    • Running Commands with Contexts
    • Integration Tests with a Local Git Server
    • Testing Commands with Mock Resources
    • Handling Signals
    • Exercises
    • Wrapping Up
  • Talking to REST APIs
  • Using the Cobra CLI Framework
  • Developing Interactive Terminal Tools
  • Working with Regular Expressions in Go
  • Distributing Your Tool
  • Author

    Ricardo Gerardi is an IT professional with over 20 years experience in large and small companies. He’s currently working as Automation Consultant at Red Hat. Ricardo has been developing command line tools for a long time, for different purposes including automation, monitoring, and data analysis tools, and he has now adopted Go for all his projects.