net/http, encoding/json, io and bufio, time, flag, and building simple HTTP services in Go.
35 cards · basic cards · AI-written, checked twice. Edit anything.
- What does http.ListenAndServe() do?
Show answer
Starts an HTTP server listening on the specified address and port, blocking until the server exits
- What function in net/http registers a handler for a URL pattern?
Show answer
http.HandleFunc(pattern, handlerFunc)
- What two methods must http.ResponseWriter implement?
Show answer
Write([]byte) and Header() http.Header
- How do you extract query parameters from a *http.Request?
Show answer
Call Request.URL.Query() to get a url.Values, then call Get() on it
- What field on *http.Request contains the HTTP method?
Show answer
The Method field (e.g., "GET", "POST", "PUT")
- What does http.Client.Do() return?
Show answer
A *http.Response and an error
- When should you use http.Client instead of http.Get()?
Show answer
When you need to set custom headers, timeouts, or other request options
- What is the http.Handler interface?
Show answer
An interface with one method: ServeHTTP(ResponseWriter, *Request)
- What constant represents the HTTP status code 200 OK?
Show answer
http.StatusOK
- What does json.Marshal() return?
Show answer
A byte slice containing JSON and an error
- What must be the second argument to json.Unmarshal()?
Show answer
A pointer to a value, usually a struct pointer
- How do you specify a custom JSON key name for a struct field?
Show answer
Use a struct tag: `json:"fieldName"`
- When should you use json.Encoder instead of json.Marshal()?
Show answer
When you are writing JSON to an io.Writer directly, like a file or HTTP response body
- When should you use json.Decoder instead of json.Unmarshal()?
Show answer
When you are reading JSON from an io.Reader, like a file or HTTP request body
- What happens to unexported struct fields when marshaling to JSON?
Show answer
They are ignored and not included in the JSON output