Developing a REST API Server

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

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Chapter 8 Talking to REST APIs | TOC | Testing the REST API Server 👉

Let’s build an API for our command-line tool to talk to. To save some time, you’ll create a REST API server that exposes data from the to-do API you developed in Defining the To-Do API. It will let users view, add, and modify to-do items using HTTP methods. You’ll reuse the code you developed before by importing the package todo from the module pragprog.com/rggo/interacting/todo.

Start by creating the directory structure for the server under your book’s root directory:

​ ​$ ​​mkdir​​ ​​-p​​ ​​$HOME/pragprog.com/rggo/apis/todoServer​
​ ​$ ​​cd​​ ​​$HOME/pragprog.com/rggo/apis/todoServer​

Then, initialize the Go module for this project:

​ ​$ ​​cd​​ ​​$HOME/pragprog.com/rggo/apis/todoServer​
​ ​$ ​​go​​ ​​mod​​ ​​init​​ ​​pragprog.com/rggo/apis/todoServer​
​ go: creating new go.mod: module pragprog.com/rggo/apis/todoServer

Next, you need to add the pragprog.com/rggo/interacting/todo module dependency to the go.mod file. In a normal workflow, with a module that’s available in a public repository, you don’t need to take additional actions. As explained in Go Modules, by running the go build or go test tools, Go would automatically download the module and add the dependency to the go.mod file. But because we’re using a…

--

--

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.