CHAPTER 23
Beginner
Building REST APIs in Go
Updated: May 17, 2026
5 min read
# CHAPTER 23
Building REST APIs in Go
1. Introduction
Go is arguably the best language in the world for building backend web servers. It powers the APIs for Netflix, Uber, and Twitch. Unlike Java or Python, which require massive third-party frameworks (like Spring Boot or Django) and heavy web servers (like Tomcat), Go can build incredibly fast, production-ready web servers using only its Standard Library.2. Learning Objectives
By the end of this chapter, you will be able to:-
Use the
net/httppackage.
- Create an HTTP Web Server.
- Define API Routes (Endpoints).
- Return JSON responses to the client.
-
Understand
http.ResponseWriterandhttp.Request.
3. The net/http Package
The net/http package provides everything needed to listen to network traffic. Let's build a server in just 10 lines of code!
go
*If you run this code, open your web browser and visit http://localhost:8080. You will see your text!*
4. Understanding Handlers
Every API endpoint needs a Handler Function. It must always have two parameters:-
1.
w http.ResponseWriter: The tool you use to write the response back to the client's browser.
-
2.
r *http.Request: A pointer holding all the details of the incoming request (the URL, headers, POST data, etc.).
5. Building a JSON REST API
Text is boring. Let's return a JSON list of users using theencoding/json skills we learned in Chapter 22.
go
*Visit http://localhost:8080/api/users and you will see a beautifully formatted JSON array!*
6. HTTP Methods (GET vs POST)
A single URL like/api/users can do different things. A GET request retrieves users. A POST request creates a new user. We check the r.Method string to determine what the user is trying to do.
go
7. Why Go Web Servers are Fast (Goroutines)
Remember Goroutines from Chapter 19? Thenet/http server has a superpower. Every time a new user sends a request to your API, the server automatically spawns a brand new Goroutine to handle it.
If 10,000 people visit your API at the exact same second, Go handles them concurrently without writing a single line of threading code!
8. Common Mistakes
-
Forgetting to set Content-Type: If you return JSON but forget
w.Header().Set("Content-Type", "application/json"), some strict front-end frameworks (like React or Angular) might fail to parse the data because they think it's plain text.
-
Writing to Headers AFTER sending data: You must configure status codes and headers *before* you use
w.Write(). Once data is sent, the headers are locked.
9. Best Practices
-
Third-Party Routers: The standard
net/httppackage is great, but routing complex URLs with variables (like/api/users/{id}) is slightly tedious. In real-world enterprise apps, developers usually import a lightweight router package like Gorilla Mux or Chi.
10. Exercises
-
1.
Create a server that listens on port
9000.
-
2.
Create an endpoint
/api/statusthat returns a JSON object{"status": "API is healthy!"}.
- 3. Test it in your browser.
11. MCQs with Answers & Explanations
Question 1
Which Go package provides the tools to build web servers and APIs?
Question 2
What does the http.ListenAndServe(":8080", nil) function do?
Question 3
An HTTP Handler function in Go MUST have which two parameters?
Question 4
What is the http.ResponseWriter used for?
Question 5
What is the *http.Request used for?
Question 6
How does the Go web server handle 1,000 simultaneous incoming requests?
Question 7
How do you instruct the browser that the data you are returning is JSON?
Question 8
Which HTTP method is traditionally used to fetch data from a REST API?
Question 9
Which HTTP method is traditionally used to submit new data (like a signup form) to an API?
Question 10
How can you check if the incoming request is a GET request?
12. Interview Preparation
Interview Questions:-
1.
Explain how Go's
net/httppackage handles concurrency under the hood. (Answer: Auto-spawning Goroutines per request).
- 2. Why is it recommended to use a router like Gorilla Mux or Chi over the standard library multiplexer for complex APIs? (Answer: Easier handling of URL path parameters and HTTP method filtering).
13. Summary
Go's built-innet/http package is an industrial-strength web server. By defining Handler functions, routing them to URLs, and leveraging the JSON package, you can build lightning-fast backend APIs. Because Go handles networking and concurrency simultaneously under the hood, your APIs are highly scalable by default.