Go > Web Development > HTTP Basics > Creating HTTP servers
Handling Multiple Routes
This snippet expands on the basic server example by demonstrating how to handle different routes (URLs) within your application. It introduces the concept of using multiple handlers to respond to different requests.
Code Snippet
This code defines two handlers: `homeHandler` for the root path ('/') and `aboutHandler` for the '/about' path. `http.HandleFunc` is used to register each handler with its respective path. When a request is made to '/', `homeHandler` is executed, and when a request is made to '/about', `aboutHandler` is executed. The `main` function starts the server and listens for incoming requests on port 8080.
package main
import (
"fmt"
"net/http"
"log"
)
func homeHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Welcome to the home page!")
}
func aboutHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "About us: We are a team of developers.")
}
func main() {
http.HandleFunc("/", homeHandler)
http.HandleFunc("/about", aboutHandler)
fmt.Println("Server starting on port 8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
Concepts Behind the Snippet
The core concept is URL routing. The HTTP server examines the URL of the incoming request and dispatches the request to the appropriate handler based on the URL's path. `http.HandleFunc` is a convenient way to register handlers for specific paths. More sophisticated routing can be achieved using a web framework.
Real-Life Use Case
In a real-world web application, you'll have multiple routes to handle different functionalities, such as displaying a homepage, showing product details, processing form submissions, and serving static files. This snippet provides the foundation for building such an application.
Best Practices
Interview Tip
Be able to explain how the server determines which handler to execute based on the URL. Also, understand the limitations of using `http.HandleFunc` directly and when a web framework might be a better choice.
When to Use Them
Use this approach when you need to handle a small number of routes and don't want the overhead of a full-fledged web framework. It's suitable for simple APIs or web applications.
Alternatives
Pros
Cons
FAQ
-
How does the server know which handler to call?
The `http.HandleFunc` function registers each handler with a specific path. When a request comes in, the server compares the request's URL path with the registered paths. If a match is found, the corresponding handler is called. -
Can I use regular expressions in the route paths?
No, `http.HandleFunc` does not support regular expressions directly. For more complex routing, you'll need to use a web framework or implement your own routing logic. -
How do I handle HTTP methods (GET, POST, etc.) differently for the same route?
You can inspect the `r.Method` field of the `http.Request` within the handler function and use a `switch` statement to handle different methods accordingly. Web frameworks often provide more convenient ways to handle method-specific routing.