Book description
Whether you want to automate tasks, analyze data, parse logs, talk to network services, or address other systems requirements, writing your own command-line tool may be the fastest - and perhaps the most fun - way to do it. The Go programming language is a great choice for developing tools that are fast, reliable, and cross-platform. Create command-line tools that work with files, connect to services, and even manage external processes, all while using tests and benchmarks to ensure your programs are fast and correct.
When you want to develop cross platform command-line tools that are fast and reliable, use Go, a modern programming language that combines the reliability of compiled languages with the ease of use and flexibility of dynamic typed languages. 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.
Publisher resources
Table of contents
- Foreword
- Acknowledgments
- Preface
- 1. Your First Command-Line Program in Go
-
2. 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
-
3. 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
- 4. Navigating the File System
- 5. Improving the Performance of Your CLI Tools
-
6. Controlling Processes
- Executing External Programs
- Handling Errors
- 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
-
7. Using the Cobra CLI Framework
- Starting Your Cobra Application
- Navigating Your New Cobra Application
- Adding the First Subcommand to Your Application
- Starting the Scan Package
- Creating the Subcommands to Manage Hosts
- Testing the Manage Hosts Subcommands
- Adding the Port Scanning Functionality
- Using Viper for Configuration Management
- Generating Command Completion and Documentation
- Exercises
- Wrapping Up
-
8. Talking to REST APIs
- Developing a REST API Server
- Testing the REST API Server
- Completing the REST API Server
- Developing the Initial Client for the REST API
- Testing the Client Without Connecting to the API
- Viewing a Single Item
- Adding an Item
- Testing HTTP Requests Locally
- Completing and Deleting Items
- Executing Integration Tests
- Exercises
- Wrapping Up
- 9. Developing Interactive Terminal Tools
- 10. Persisting Data in a SQL Database
-
11. Distributing Your Tool
- Starting the Notify Package
- Including OS-Specific Data
- Including OS-Specific Files in the Build
- Testing the Notify Package
- Conditionally Building Your Application
- Cross-Compiling Your Application
- Compiling Your Go Application for Containers
- Distributing Your Application as Source Code
- Exercises
- Wrapping Up
Product information
- Title: Powerful Command-Line Applications in Go
- Author(s):
- Release date: December 2021
- Publisher(s): Pragmatic Bookshelf
- ISBN: 9781680509328
You might also like
book
Building Modern CLI Applications in Go
Evolve the humble CLI using Go and unleash the next generation of powerful, flexible, and empathy-driven …
video
Up and Running with Concurrency in Go (Golang)
According to the most recent developer surveys, Go(lang) is one of the world’s most desired and …
book
Efficient Linux at the Command Line
Take your Linux skills to the next level! Whether you're a system administrator, software developer, site …
book
Concurrency in Go
Concurrency can be notoriously difficult to get right, but fortunately, the Go open source programming language …