Powerful Command-Line Applications in Go
Build Fast and Maintainable Tools
by Ricardo Gerardi
Version: P1.0 (December 2021)
Copyright 2021 The Pragmatic Programmers, LLC. This book is licensed to the individual who purchased it. We don't copy-protect it because that would limit your ability to use it for your own purposes. Please don't break this trustyou can use this across all of your devices but please do not share this copy with other members of your team, with friends, or via file sharing services. Thanks.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC.
Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein.
About the Pragmatic Bookshelf
The Pragmatic Bookshelf is an agile publishing company. Were here because we want to improve the lives of developers. We do this by creating timely, practical titles, written by programmers for programmers.
Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://pragprog.com.
Our ebooks do not contain any Digital Restrictions Management, and have always been DRM-free. We pioneered the beta book concept, where you can purchase and read a book while its still being written, and provide feedback to the author to help make a better book for everyone. Free resources for all purchasers include source code downloads (if applicable), errata and discussion forums, all available on the book's home page at pragprog.com. Were here to make your life easier.
New Book Announcements
Want to keep up on our latest titles and announcements, and occasional special offers? Just create an account on pragprog.com (an email address and a password is all it takes) and select the checkbox to receive newsletters. You can also follow us on twitter as @pragprog.
About Ebook Formats
If you buy directly from pragprog.com, you get ebooks in all available formats for one price. You can synch your ebooks amongst all your devices (including iPhone/iPad, Android, laptops, etc.) via Dropbox. You get free updates for the life of the edition. And, of course, you can always come back and re-download your books when needed. Ebooks bought from the Amazon Kindle store are subject to Amazon's polices. Limitations in Amazon's file format may cause ebooks to display differently on different devices. For more information, please see our FAQ at pragprog.com/#about-ebooks. To learn more about this book and access the free resources, go to https://pragprog.com/book/rggo, the book's homepage.
Thanks for your continued support,
Andy Hunt
The Pragmatic Programmers
The team that produced this book includes: Dave Rankin (CEO) Janet Furlow (COO) Tammy Coron (Managing Editor) Brian P. Hogan (Development Editor) Corina Lebegioara (Copy Editor) Potomac Indexing, LLC (Indexing) Gilson Graphics (Layout) Andy Hunt and Dave Thomas (Founders)
For customer support, please contact .
For international rights, please contact .
To my beloved wife Kassia. My best friend, my greatest supporter. Sadly she passed away due to cancer before seeing this book completed.
To my four incredible daughters Gisele, Livia, Elena, and Alice. The reason for everything.
Table of Contents
Copyright 2021, The Pragmatic Bookshelf.
Early Praise for Powerful Command-Line Applications in Go
Ricardo packs a lot of practical information in this book. Right off the bat he starts with a complete example of a command-line application, from coding, to testing to deploying. This book will show you many practical ways to work with Go and write command-line applications.
Miki Tebeka |
CEO, 353solutions |
Go is great at creating command-line tools and Ricardo does a great job at explaining how to develop command-line utilities in Go. All kinds of Go programmers who read this book are going to learn how to develop powerful command-line tools easily and productively.
Mihalis Tsoukalos |
Author, Mastering Go |
The best Go book Ive read so far. I love that examples are something useful, not abstractions. I recommend the book for those who want to learn Go or who already have some knowledge. Im sure everyone will learn something valuable.
Renato Suero |
Senior Software Engineer, Proposify |
Foreword
In 2012 I began a journey of experimentation in investigating a new language,Go, which was born at Google and had just reached the 1.0 milestone. I learnbest by building so I was looking for a project meaningful enough to actuallylearn Go. I was growing frustrated with the increasing cost andcomplexity of my WordPress-powered blog with entirely static content anddecided that building a static site generator was the project for me to learnGo. I began writing my first Go project, Hugo.
Having previously designed several CMSs and command-line tools, I had a goodsense of what I wanted to build. I opened up a terminal and began by typingcommands for this not yet existing program to effectively sketch out how theuser interface would be shaped. With this sketch in hand, I then began theprocess of building the application. As I was inexperienced with Go, I hoped tolean heavily on existing libraries, but as this was the dawn of the Goecosystem, more often than not, the libraries I needed didnt exist.
Unable to find the right library to support the design pattern of [application] [command] [flag] [argument], I set out to write it myself. I also needed config file management as there was too much configurability to anticipate everything passed via the command line. As my goal was building Hugo, these were just Hugo packages initially, but I thought maybe someone else would benefit from this functionality too, and I pulled them out into standalone libraries and named them Cobra of this book.
Through the experience of building Hugo, Cobra, and Viper, I had arevelation: while Go was initially targeted at large-scale server applications,the Go creators had developed a language perfectly suited for command-linetools. It had everything you needed and most of what you wanted:
Static compilation, which created executables with no local dependencies: no runtimes or libraries needed. What could be easier to install than one file?
Cross-compilation, eliminating the need for build farms.
Lightning-fast builds so quick that it felt like a dynamic language.
Native concurrency, enabling your applications to take full advantage of multicore machines.
Go had nearly all the advantages of a dynamic language during development and all the advantages of a compiled language during execution, plus additional unique advantages. In short, Go is uniquely perfect for both building and running command-line applications.