Menu Close

Go – How to get DNS TXT records of domain name in Go

Here, we will help you to understand how to get DNS TXT records of domain name in Go. We will learn about LookupTXT() method to get DNX TXT records in go.

Function prototype:

func LookupTXT(name string) ([]string, error)

Input parameters:

Name: Domain name

Return value:

LookupTXT() returns the DNS TXT records for the given domain name.

Example with code:

package main

import (
  "fmt"
  "net"
)


func main() {

    dns_txt_records, err := net.LookupTXT("google.com")

    if err == nil {

        fmt.Println("DNS TXT record: ")

        for _, record := range(dns_txt_records) {

            fmt.Println(record)

        }

    } else {

        fmt.Println("Lookup DNS TXT records failed. Error is: ", err)

    }

}

Output:

$ go run sample.go

DNS TXT record:

globalsign-smime-dv=CDYX+XFHUw2wml6/Gb8+59BsH31KzUr6c1l2BPvqKX8=
v=spf1 include:_spf.google.com ~all
facebook-domain-verification=22rm551cu4k0ab0bxsw536tlds4h95
docusign=1b0a6754-49b1-4db5-8540-d2c12664b289
MS=E4A68B9AB2BB9670BCE15412F62916164C0B20BB
google-site-verification=TV9-DBe4R80X4v0M4U_bd_J9cpOJM0nikft0jAgjmsQ
apple-domain-verification=30afIBcvSuDV2PLX
google-site-verification=wD8N7i1JTNTkezJ49swvWW48f8_9xveREV4oB-0Hf5o
docusign=05958488-4752-4ef2-95eb-aa7ba8a3bd0e

Example with invalid domain name:

package main

import (
  "fmt"
  "net"
)


func main() {

    dns_txt_records, err := net.LookupTXT("invalidDomain123.com")

    if err == nil {

        for _, record := range(dns_txt_records) {

            fmt.Println("DNS TXT record: ", record)

        }

    } else {

        fmt.Println("Lookup DNS TXT records failed. Error is: ", err)

    }

}

Output:

$: go run sample.go

Lookup DNS TXT records failed. Error is:  lookup invalidDomain123.com on 10.138.9.15:53: no such host

To learn more about golang, Please refer given below link:

https://techieindoor.com/go-lang-tutorial/

References:

https://golang.org/doc/
https://golang.org

Posted in golang, net

Leave a Reply

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