yoklainterview sim

Backend Go Stdlib Http Interview Questions

75 verified Backend Go Stdlib Http interview questions — solve with answers, learn from explanations, test yourself in a real simulation.

Try the real simulation →

Sample questions

Go Stdlib HttpDifficulty 1
In net/http, http.HandlerFunc is a defined function type. Which statement about it is correct?
  • aIt adapts any func(http.ResponseWriter, *http.Request) into an http.Handler via a ServeHTTP method
  • bIt requires the wrapped function to also implement Read and Write methods
  • cIt only works for functions that return an error value
  • dIt automatically registers the function with the default ServeMux
Explanation:http.HandlerFunc(f) is a type with a ServeHTTP method that just calls f(w, r); it's the standard adapter for turning a plain function into an http.Handler. It does not require Read/Write, does not require an error return, and does not register anything by itself — http.HandleFunc or mux.HandleFunc does the registering.
Go Stdlib HttpDifficulty 3
On a classic http.ServeMux (patterns without HTTP methods, pre-1.22 style), you register mux.HandleFunc("/users", usersHandler) and mux.HandleFunc("/", rootHandler). A request arrives for path "/users/42". Which handler runs?
  • ausersHandler runs, matching "/users" as a prefix
  • brootHandler runs, via the "/" catch-all pattern
  • cNeither handler runs; ServeMux returns 404
  • dusersHandler runs, via the longest matching pattern
Explanation:Classic ServeMux rules: a pattern ending in "/" is a subtree match covering everything under it, while a pattern without the trailing slash matches that exact path only. "/users" has no trailing slash, so it does not match "/users/42"; the request falls back to the registered "/" pattern, which matches any path not matched more specifically.
Go Stdlib HttpDifficulty 2
func handler(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("hello"))
    w.WriteHeader(http.StatusTeapot)
}

What status code does the client actually receive?
  • a418 I'm a Teapot
  • b500 Internal Server Error
  • c200 OK
  • dNo response is sent; the connection closes without a status line
Explanation:If a handler calls Write before ever calling WriteHeader, net/http sends an implicit WriteHeader(http.StatusOK) first. Once the header section is written, further calls to WriteHeader are ignored (with a warning logged), so the client sees 200 OK regardless of the later w.WriteHeader(http.StatusTeapot) call.
Go Stdlib HttpDifficulty 2
func handler(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(http.StatusOK)
    w.Header().Set("X-Custom", "value")
}

Will the client receive the X-Custom header?
  • aYes, at any point during the handler
  • bYes, but only if the body is left empty
  • cNo, only because "X-Custom" starts with an uppercase letter
  • dNo, the header section was already sent
Explanation:w.Header() returns the map that backs the response headers, but it is only read and sent when WriteHeader (or the first Write) actually flushes headers to the wire. Any Header().Set call after that point mutates a map that is no longer consulted, so the change is silently dropped.
Go Stdlib HttpDifficulty 2
After an http.HandlerFunc returns, is it the handler's responsibility to explicitly call r.Body.Close()?
  • aNo, the net/http server always closes it
  • bYes, otherwise the TCP connection is never released
  • cYes, otherwise json.NewDecoder will panic next time
  • dNo, but only for GET requests without a body
Explanation:net/http guarantees that the Server closes the request body once the handler function returns, whether or not the handler itself read it. Calling r.Body.Close() explicitly is harmless and sometimes done for clarity, but it's not required to avoid a resource leak in the standard server.
Go Stdlib HttpDifficulty 2
type payload struct {
    Name string `json:"name"`
    age  int    `json:"age"`
}
var p payload
json.NewDecoder(r.Body).Decode(&p)

Given request body {"name":"Ali","age":30}, what is p.age after Decode?
  • a30, since the json tag makes the field addressable
  • b0, its zero value
  • cDecode returns an error
  • d30, but only because the struct was passed by pointer
Explanation:encoding/json uses reflection, which cannot set unexported struct fields from outside the package. The age field is lowercase (unexported), so Decode silently skips it — no error, but also no assignment — leaving p.age at its zero value 0.

Test yourself against the 3300-question Backend bank.

Start interview