How to Validate a Domain Name in Go

If you or any of your peers have ever been the target of a phishing attempt, you know that it can be dangerous to click on an unknown domain name; with one wrong click, the security of you or your company’s information can be infiltrated. By using the following API in Go, you can instantly check whether a domain name is valid; the API does the work for you by connecting with DNS services to perform a live validation.

All you need for this simple operation is the target domain name, which you can input into the following code:

package mainimport (
"fmt"
"strings"
"net/http"
"io/ioutil"
)
func main() {url := "https://api.cloudmersive.com/validate/domain/check"
method := "POST"
payload := strings.NewReader(`"<string>"`)client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Apikey", "YOUR-API-KEY-HERE")
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}

If you have any questions, just head over to the Cloudmersive website and we can assist. You can also retrieve your API key from the site by registering for a free account; this will give you access to 800 monthly calls across our library of APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store