In this article, we will explore the http HandleFunc function in the Go net/http package in detail, along with examples.
Introduction:
Go, also known as Golang, is a statically-typed, compiled programming language designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson. The language has gained popularity in recent years, thanks to its simplicity, efficiency, and built-in support for concurrent programming. Go provides an extensive standard library, which includes the net/http
package, essential for building web applications and APIs.
Overview of http.HandleFunc
The http.HandleFunc
function is used to register a function to handle incoming HTTP requests for a specific route or path. The function takes two arguments:
- A string representing the URL pattern
- A function that has the signature
func(http.ResponseWriter, *http.Request)
The registered function is called a “handler” and will be invoked whenever an incoming request matches the specified URL pattern. The handler function receives two arguments: the first is an http.ResponseWriter
interface to write the response, and the second is a pointer to an http.Request
object representing the incoming request.
syntax
func HandleFunc(pattern string, handler func(ResponseWriter, *Request))
Creating a Simple HTTP Server with HandleFunc.
To understand the usage of http.HandleFunc
, let’s create a simple HTTP server that responds to different URL paths.
package main import ( "fmt" "net/http" ) func main() { http.HandleFunc("/", homeHandler) http.HandleFunc("/hello", helloHandler) fmt.Println("Starting the server on :8080...") http.ListenAndServe(":8080", nil) } func homeHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Welcome to the Home Page!") } func helloHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, Gophers!") }
In this example, we define two handler functions, homeHandler
and helloHandler
, which handle the incoming requests for the “/” and “/hello” paths, respectively. We register these functions using http.HandleFunc
and start the HTTP server on port 8080 using http.ListenAndServe
.
To test the server, open your browser and visit http://localhost:8080 and http://localhost:8080/hello to see the responses generated by our handler functions.
Understanding the http.ResponseWriter and *http.Request
The http.ResponseWriter
interface represents the server’s side of an HTTP response, and it allows you to write the response headers, status code, and the body.
Some commonly used methods of http.ResponseWriter
include:
Header() http.Header
: Returns the response header map.Write([]byte) (int, error)
: Writes the data to the connection as part of an HTTP reply.WriteHeader(statusCode int)
: Sends an HTTP response header with the provided status code.
The http.Request
struct represents an HTTP request received by the server. It contains fields such as Method
, URL
, Header
, Body
, and more, which provide detailed information about the incoming request.
Conclusion
In this article, we covered the http.HandleFunc
function in the Go net/http
package. This function is a powerful yet straightforward tool to register custom handlers for specific URL patterns in your web application or API. We also discussed the http.ResponseWriter
and http.Request
objects, which are essential components of any Go HTTP handler. With this knowledge in hand, you’re well on your way to building robust and scalable web.
To check more leetcode problem’s solution. Pls click given below link:
https://techieindoor.com/category/leetcode/