Golang Program to Check Status of a URL_Website


When building web applications, it's essential to ensure that all URLs and websites are functional and accessible to users. Checking the status of a URL or website is crucial in determining whether there is an issue that needs to be addressed. In this article, we will discuss how to write a Golang program to check the status of a URL/website.

What is URL/Website Status?

The status of a URL or website is a representation of its accessibility and functionality. URLs or websites can have different status codes depending on the outcome of the request. For example, a status code of 200 indicates that the URL or website is accessible and functioning correctly, while a status code of 404 indicates that the URL or website is not found.

Steps to Check Status of a URL/Website

In Golang, we can check the status of a URL or website by sending an HTTP request and examining the response. Here are the steps to check the status of a URL or website.

Step 1: Import the Net/HTTP Package

To make HTTP requests in Golang, we need to import the "net/http" package.

import "net/http"

Step 2: Send an HTTP Request

After importing the "net/http" package, we can send an HTTP request to the URL or website we want to check. Here's how we can send an HTTP request.

func checkStatus(url string) string {
   response, err := http.Get(url)
   if err != nil {
      return err.Error()
   }
   defer response.Body.Close()
   return response.Status
}

In the above code, we have defined a function called "checkStatus" that takes a URL as an argument and returns a string. The function sends an HTTP GET request to the URL and returns the status of the response. If the request returns an error, the function returns the error message.

Step 3: Test the Function

We can test the "checkStatus" function with different URLs to check if it is working correctly. Here's how we can test the function.

func main() {
   url1 := "https://www.google.com"
   url2 := "https://www.nonexistenturl.com"
   fmt.Println(checkStatus(url1)) // Output: 200 OK
   fmt.Println(checkStatus(url2)) // Output: Get "https://www.nonexistenturl.com": dial tcp: lookup www.nonexistenturl.com: no such host
}

In the above code, we have defined two URLs, url1 and url2, and passed them to the checkStatus function. The function returns the status of the response for url1, which should be 200 OK, indicating that the URL is accessible and functioning correctly. For url2, the function returns an error message because the URL is not found.

Example

package main

import (
   "fmt"
   "net/http"
)

func checkStatus(url string) string {
   response, err := http.Get(url)
   if err != nil {
      return err.Error()
   }
   defer response.Body.Close()
   return response.Status
}

func main() {
   url1 := "https://www.google.com"
   url2 := "https://www.nonexistenturl.com"
   fmt.Println(checkStatus(url1)) // Output: 200 OK
   fmt.Println(checkStatus(url2)) // Output: Get "https://www.nonexistenturl.com": dial tcp: lookup www.nonexistenturl.com: no such host
}

Output

Get "https://www.google.com": dial tcp: lookup www.google.com on 185.12.64.1:53: dial udp 185.12.64.1:53: socket: permission denied
Get "https://www.nonexistenturl.com": dial tcp: lookup www.nonexistenturl.com on 185.12.64.1:53: dial udp 185.12.64.1:53: socket: permission denied

Conclusion

In this article, we have discussed how to write a Golang program to check the status of a URL or website. We have used the "net/http" package to send an HTTP request to the URL and examine the response to determine the status. By following the above steps, you can easily check the status of any URL or website in Golang. This can be useful for ensuring that all URLs and websites in your web application are accessible and functioning correctly.

Updated on: 19-Apr-2023

577 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements