Creating the Initial To-Do Command-Line Tool

Powerful Command-Line Applications in Go — by Ricardo Gerardi (20 / 127)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Defining the To-Do API | TOC | Testing the Initial CLI Implementati on 👉

You have a working to-do API, so now you can build a command-line interface on top of it. We’ll start with an initial implementation that includes the following two features:

  1. When executed without any arguments, the command will list the available to-do items.
  2. When executed with one or more arguments, the command will concatenate the arguments as a new item and add it to the list.

Start by creating the file main.go in the cmd/todo directory as described in Organizing Your Code.

​ ​$ ​​cd​​ ​​$HOME/pragprog.com/rggo/interacting/todo/cmd/todo​

Add the following code to the main.go file to define the package and imports you’ll use:

interacting/todo/cmd/todo/main.go

​ ​package​ main

​ ​import​ (
​ ​"fmt"​
​ ​"os"​
​ ​"strings"​

​ ​"pragprog.com/rggo/interacting/todo"​
​ )

You’ll use the Args variable from the os package to verify the arguments provided during your tool’s execution. You’ll use the fmt and strings packages to process input and output.

Similarly to what you’ve done in the todo_test.go file, you’re importing your own todo package to use the to-do functionality.

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.