Open In App

Golang Program to Check Status of a URL/Website

Improve
Improve
Like Article
Like
Save
Share
Report

Here, we will check the status of a URL using Golang. First, we will import the simple fmt package in Golang and then store the input in a variable of type string using the scan function. 

Go




// Go program to enter URL
package main
 
import (
    "fmt"
)
func main() {
    var url string
    fmt.Print("Enter the URL of a website: ")
    fmt.Scan(&url)
}


Create a file, it can be any name you like but it should have a .go extension. You can run the script by executing the command:

go run filename.go

In this script, we have declared a variable URL of the type string. We simply display an informative message for the input which is taken using the Scan function and stored in the URL variable. 

Using Get Function in HTTP 

Now we have a URL in a variable, we can set up the HTTP module in Golang to make functions to fetch website data or simply the metadata. We need to import the “net/http” module into our program.

After the import of the HTTP module, we can access the Get function provided in the library, so using the Get method we can simply fetch the response by sending a GET request to the provided URL. The Get function takes an argument as the URL of the website and returns the response else it returns an error which might be a case if the URL doesn’t exist or is invalid. 

Go




// Go program to enter URL
package main
 
import (
    "fmt"
    "net/http"
)
func main() {
    var url string
    fmt.Print("Enter the URL of a website: ")
    fmt.Scan(&url)
    resp,err := http.Get(url)
    fmt.Print(resp)
    fmt.Print(err)
}


The program can be improved further by adding an if-else condition for error checking and exit status of the program. So we can log the error from the Get function call using the log module in Golang. 

Go




// Go program to enter URL
package main
 
import (
    "fmt"
    "net/http"
    "log"
)
func main() {
    var url string
    fmt.Print("Enter the URL of a website: ")
    fmt.Scan(&url)
    resp,err := http.Get(url)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Print(resp)
}


Output:

 

As we can see we have a response header from the given URL. We will now see how to get the status code from the received response object. 

Checking the Response Status Code

Finally, we need to check the status code of the response received after sending the GET request to the provided URL. We will simply print the status code of the URL and that can be further interpreted by the user or modified as per requirements like redirected, success, internal error, or not found. 

Go




// Go program checking the response status code
package main
 
import (
    "fmt"
    "net/http"
    "log"
)
 
func main() {
    var url string
    fmt.Print("Enter the URL of a website: ")
    fmt.Scan(&url)
    resp,err := http.Get(url)
    if err != nil {
        log.Fatal(err)
    }
    status_code := resp.StatusCode
    fmt.Println(status_code,":",url)
}


In the above, script we can access the status code from the response by using the dot prefix with the name Status Code and simply storing it in the status_code variable. 

 

Script demonstration



Last Updated : 03 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads