build-an-http-server / lesson-18.md
Lesson 18 · Building responses

Format the status line

Every response opens with a status line naming the version, a numeric code, and a human reason phrase. Today you format that line from a code, backed by a small table of standard reasons.

The goal

Turn a status code into a full status line, looking its reason phrase up from a table.

Start here - the target
TO DO
Scenario: Formatting the status line
Giventhe status code 200
Whenthe status line is formatted
Thenit is "HTTP/1.1 200 OK\r\n"
Andthe code 404 gives "HTTP/1.1 404 Not Found\r\n"
Background

A response mirrors a request: it starts with one line, then headers, then a blank line, then the body. The status line is HTTP/1.1, a three-digit status code, and a short reason phraseOK, Not Found — that exists purely for humans reading the wire.

Back the reason phrases with a lookup table, and seed it now with the codes this chapter will use (200, 400, 404, 405, 500). Building the table once, even before every code has a caller, means later lessons just add a status number and get a correct line for free.

Make it work
var reasons = map[int]string{
200: "OK", 400: "Bad Request", 404: "Not Found",
405: "Method Not Allowed", 500: "Internal Server Error",
}
line := fmt.Sprintf("HTTP/1.1 %d %s\r\n", code, reasons[code])
CheckpointDONE
A status code formats into a full status line. Commit.