Book Image

Building Modern CLI Applications in Go

By : Marian Montagnino
Book Image

Building Modern CLI Applications in Go

By: Marian Montagnino

Overview of this book

Although graphical user interfaces (GUIs) are intuitive and user-friendly, nothing beats a command-line interface (CLI) when it comes to productivity. Many organizations settle for a GUI without searching for alternatives that offer better accessibility and functionality. If this describes your organization, then pick up this book and get them to rethink that decision. Building Modern CLI Applications in Go will help you achieve an interface that rivals a GUI in elegance yet surpasses it in high-performance execution. Through its practical, step-by-step approach, you’ll learn everything you need to harness the power and simplicity of the Go language to build CLI applications that revolutionize the way you work. After a primer on CLI standards and Go, you’ll be launched into tool design and proper framework use for true development proficiency. The book then moves on to all things CLI, helping you master everything from arguments and flags to errors and API calls. Later, you’ll dive into the nuances of empathic development so that you can ensure the best UX possible, before you finish up with build tags, cross-compilation, and container-based distribution. By the end of this UX book, you’ll be fully equipped to take the performance and flexibility of your organization’s applications to the next level.
Table of Contents (21 chapters)
1
Part 1: Getting Started with a Solid Foundation
6
Part 2: The Ins and Outs of a CLI
10
Part 3: Interactivity and Empathic Driven Design
14
Part 4: Building and Distributing for Different Platforms

Interacting with REST APIs

Often, if a company or user has already created an API, the command-line application will send requests to either the REST API or the gRPC endpoints. Let’s first talk about using REST API endpoints. It is important to understand the net/http package. It’s quite a large package with many types, methods, and functions, many of which are used for development on the server side. In this context, the command-line application will be the client of the API, so we won’t discuss each in detail. We’ll go into a few basic use cases from the client side though.

Get request

Let’s revisit the code from Chapter 3, Building an Audio Metadata CLI. Within the Run command of the CLI command code, found in the /cmd/cli/command/get.go file, is a snippet of code that calls the corresponding API request endpoint using the GET method:

params := "id=" + url.QueryEscape(cmd.id)
path := fmt.Sprintf("http://localhost/request...