web application basics Flashcards
- What is the purpose of the net/http package?
It provides HTTP client and server implementations for building web applications in Go.
- How do you start an HTTP server in Go?
By using http.ListenAndServe with a specified address and handler.
- What is a ServeMux in Go?
It is an HTTP request multiplexer that matches the URL of each incoming request against a list of registered patterns and calls the corresponding handler.
- How do you register a handler function for a specific route in Go?
By using http.HandleFunc with the route pattern and handler function.
- What is the purpose of the http.ResponseWriter interface?
It assembles the HTTP server’s response by writing header and body data.
- How can you serve static files in Go?
By using http.FileServer and http.StripPrefix to handle requests for static assets.
- What is the role of the http.Handler interface?
It defines a method to serve HTTP requests and is implemented to create custom handlers.
- How do you implement the http.Handler interface in Go?
By creating a struct with a ServeHTTP method that takes http.ResponseWriter and *http.Request as parameters.