ProgramGuru

Building REST APIs

Start with Building REST APIs

In this lesson, explore building rest apis through one small Go program, and connect its output to the language rules behind it.

What you’ll learn

  • Explain the main purpose of building rest apis.
  • Read and run the complete Go example in this lesson.
  • Apply the concept and recognize common mistakes.

Understand the idea

REST-style APIs identify resources by URLs and use HTTP methods to express operations. A task resource might support GET /tasks/5 and DELETE /tasks/5.

A good contract also defines error responses, validation, idempotency, authentication and data ownership—not only URLs.

Visualize how it works

Follow this diagram before executing the example. Check where a value or decision changes what runs next.

Visual explanation for restMethod and resource name together form the API contract.Resource /tasks/5Step 1Choose HTTP methodStep 2Route to operationStep 3Return statusStep 4Method and resource name together form the API contract.
Method and resource name together form the API contract.

Run a complete Go example

This example is a complete program. Save it as main.go or paste it into the Go Playground.

Go code

package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"
)

func main() {
	mux := http.NewServeMux()
	mux.HandleFunc("GET /tasks/{id}", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "Read task ", r.PathValue("id")) })
	mux.HandleFunc("DELETE /tasks/{id}", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) })
	for _, method := range []string{"GET", "DELETE"} {
		rec := httptest.NewRecorder()
		mux.ServeHTTP(rec, httptest.NewRequest(method, "/tasks/5", nil))
		fmt.Println(method, rec.Code)
	}
}

Output

GET 200
DELETE 204

Trace what the program does

The same URL is routed according to its HTTP method. GET returns 200 after writing a body; DELETE explicitly returns 204.

Avoid common mistakes

  • A DELETE endpoint should be designed for safe repeat requests, not blind duplicate effects.
  • HTTP methods are semantics, not sufficient authorization on their own.

Try it yourself

Add a POST /tasks route that returns 201 Created and a JSON response.

Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.

Check your understanding

What does REST generally use to represent operations on resources?
Choose one answer. You can try again.

Answer and explanation

HTTP methods on resource URLs

Correct. The same URL is routed according to its HTTP method.

What you should remember

You have practiced Building REST APIs in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.

Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Middleware in Go →