Menu Close

Go – HTTP ListenAndServe Function in Go

In this article, we will explore the http ListenAndServe function in 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. It is known for its simplicity, strong concurrency support, and efficient performance. In this article, we will explore one of Go’s key features, the http.ListenAndServe function, which allows developers to create HTTP servers with ease.

What is the http.ListenAndServe function ?

The http.ListenAndServe function in Go is a part of Go’s standard library in the net/http package. It provides an easy way to create an HTTP server that listens for incoming connections and serves HTTP responses. This function starts an HTTP server on a specified address and port, and routes incoming requests to the appropriate handlers.

syntax

func ListenAndServe(addr string, handler Handler) error

Parameters:

  • addr: The address and port on which the server should listen for incoming connections. For example, “localhost:8080” or “:8080”.
  • handler: An object that implements the http.Handler interface, which is responsible for handling incoming requests and generating responses. If nil, the http.DefaultServeMux is used.

Let’s break down the http.ListenAndServe function with an example.

Example:

package main

import (
	"fmt"
	"net/http"
)

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Welcome to the Golang HTTP Server!")
	})

	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		fmt.Printf("Failed to start server: %v\n", err)
	}
}

Explanation:

  1. Import the net/http and fmt packages.
  2. Inside the main function, use the http.HandleFunc function to register a function that handles HTTP requests for the root path (“/”). This function takes in two parameters: the path pattern and the function to handle requests matching that pattern.
  3. Define an anonymous function with http.ResponseWriter and *http.Request parameters. This function writes the “Welcome to the Golang HTTP Server!” message to the response writer.
  4. Call http.ListenAndServe with the address “:8080” and a nil handler, which means that the http.DefaultServeMux will be used for routing requests. The server will now listen for incoming connections on port 8080 and respond to requests for the root path.
  5. Handle any errors returned by http.ListenAndServe, printing an error message if the server fails to start.

Conclusion

The http.ListenAndServe function is a convenient way to create HTTP servers in Go. By leveraging the net/http package, you can quickly set up servers with minimal code and effort. This powerful functionality allows you to create everything from simple APIs to full-fledged web applications, making Go an excellent choice for modern web development.

To check more leetcode problem’s solution. Pls click given below link:

https://www.techieindoor.com/category/leetcode/

Posted in golang, net, packages

Leave a Reply

Your email address will not be published. Required fields are marked *