StatusText Maps Codes To Reason Phrases

http.StatusText turns numeric status codes into canonical reason phrases and returns an empty string for unregistered codes like 599.

GoCli Tooling

Code

snippet
package main

import (
	"fmt"
	"net/http"
)

func main() {
	// StatusText maps status codes back to canonical reason phrases
	for _, code := range []int{
		http.StatusContinue,
		http.StatusOK,
		http.StatusMovedPermanently,
		http.StatusBadRequest,
		http.StatusUnauthorized,
		http.StatusNotFound,
		http.StatusInternalServerError,
		http.StatusBadGateway,
	} {
		fmt.Println(code, "->", http.StatusText(code))
	}

	// unregistered codes yield the empty string, no invented text
	fmt.Println("unknown:", http.StatusText(599))
}

What the demo maps

The loop walks eight well-known codes, 100 through 502, and prints each beside http.StatusText of it, producing the canonical reason phrases Continue, OK, Moved Permanently, Bad Request, Unauthorized, Not Found, Internal Server Error and Bad Gateway straight from net/http.

The last line asks for 599, which no standard registers. StatusText returns the empty string rather than inventing text, so log rendering can treat an empty phrase as an unknown code instead of trusting a fabricated label.

Points to remember

  • The mapping is the same table servers use to write status lines.
  • Custom codes stay printable; the demo logs 599 against an empty phrase.

Interactive Tools for This Topic

FAQ

What does StatusText return for an unregistered code?
An empty string; 599 is not in the standard table, so nothing is invented and the demo prints the empty result explicitly.