Implementing a Basic HTTP Handler in Go
This challenge will guide you through the process of creating a custom HTTP handler function in Go. Understanding how to define and use http.HandlerFunc is fundamental for building any web service or API in Go. You'll learn how to respond to incoming HTTP requests and send back simple text responses.
Problem Description
Your task is to create an http.HandlerFunc that will serve as an endpoint for a simple web server. When a GET request is received at the /hello path, your handler should respond with the text "Hello, World!".
Key Requirements:
- Implement a function that conforms to the
http.HandlerFuncsignature:func(w http.ResponseWriter, r *http.Request). - This handler function should only respond to GET requests.
- For GET requests to the
/hellopath, it should write the string "Hello, World!" to thehttp.ResponseWriter. - For any other HTTP method or path, it should return an appropriate HTTP error (e.g., 405 Method Not Allowed or 404 Not Found).
Expected Behavior:
- When a client makes a
GETrequest to/hello, the server should respond with:- HTTP Status Code:
200 OK - Body:
Hello, World!
- HTTP Status Code:
- When a client makes a
POSTrequest to/hello, the server should respond with:- HTTP Status Code:
405 Method Not Allowed - Body: (Optional, but good practice to include a message like "Method not allowed")
- HTTP Status Code:
- When a client makes a
GETrequest to/other, the server should respond with:- HTTP Status Code:
404 Not Found - Body: (Optional, but good practice to include a message like "Not found")
- HTTP Status Code:
Edge Cases to Consider:
- Requests with methods other than GET to the
/hellopath. - Requests to paths other than
/hello.
Examples
Example 1:
- Input (Client Request):
GET /hello HTTP/1.1 Host: localhost:8080 - Output (Server Response):
HTTP/1.1 200 OK Content-Type: text/plain; charset=utf-8 X-Content-Length: 13 Date: [Current Date/Time] Hello, World! - Explanation: The
GETrequest to/hellois handled by your customHandlerFunc, which correctly writes "Hello, World!" and sets the status to200 OK.
Example 2:
- Input (Client Request):
POST /hello HTTP/1.1 Host: localhost:8080 - Output (Server Response):
HTTP/1.1 405 Method Not Allowed Content-Type: text/plain; charset=utf-8 X-Content-Length: 17 Date: [Current Date/Time] Method not allowed - Explanation: The
POSTrequest to/hellois not handled by the specific logic for GET requests, so the handler should inform the client that the method is not allowed.
Example 3:
- Input (Client Request):
GET /goodbye HTTP/1.1 Host: localhost:8080 - Output (Server Response):
HTTP/1.1 404 Not Found Content-Type: text/plain; charset=utf-8 X-Content-Length: 9 Date: [Current Date/Time] Not found - Explanation: The
GETrequest to/goodbyeis not the/hellopath, so the handler should indicate that the resource was not found.
Constraints
- The Go standard library
net/httppackage must be used. - No external third-party HTTP routing libraries are allowed.
- The solution should be contained within a single
mainpackage. - The HTTP server should listen on port
8080.
Notes
- Remember to import the
net/httpandfmtpackages. - You will need to register your
http.HandlerFuncwith a specific path usinghttp.HandleFunc. - You will then need to start the HTTP server using
http.ListenAndServe. - Use
w.WriteHeader()to set the HTTP status code. - Use
fmt.Fprintln()orw.Write()to write the response body. - For error responses, consider setting the appropriate
Content-Typeheader.