Golang DNS Lookup: CNAME, MX, NS, TXT & SRV

Golang DNS lookup examples


golang dns lookupGolang DNS lookup is simple using the standard net package. The Golang net package includes several dns lookup methods that either use the C stdlib via cgo, or a pure Go DNS resolver.

Using Golang as a DNS client can be achieved by following the examples below.

Overview

LookupAddr

LookupAddr does a reverse lookup for the address and returns a list of names that map to the address.

LookupCNAME

LookupCNAME returns the final name after following zero or more CNAME records.

LookupHost

LookupHost returns the host addresses using the local resolver.

LookupIP

LookupIP returns a hosts IP addresses. It is now preferred over the Hosts method to resolve a name to IP.

LookupMX

LookupMX returns the DNS MX records for the domain name, sorted by preference.

LookupNS

LookupNS returns DNS NS records for the domain name.

LookupSRV

LookupSRV attempts to resolve an SRV query of the service, protocol (tcp or udp), and domain name, sorted by priority and randomized by weight within a priority.

LookupTXT

LookupTXT returns DNS TXT records for the domain name.

See also:
Golang SSH Client
Golang IP Address Manipulation
Golang Regular Expression Match