Looking up MX records

This program will take a domain name and return the MX records. MX records, or mail exchanger records, are DNS records that point to the mail server. For example, the MX server of https://www.devdungeon.com/ is mail.devdungeon.com. The net.LookupMX() function performs this lookup and returns a slice of the net.MX structs:

package mainimport (   "fmt"   "log"   "net"   "os")func main() {   if len(os.Args) != 2 {      log.Fatal("No domain name argument provided")   }   arg := os.Args[1]     fmt.Println("Looking up MX records for " + arg)     mxRecords, err := net.LookupMX(arg)   if err != nil {      log.Fatal(err)   }   for _, mxRecord := range mxRecords {      fmt.Printf("Host: %s\tPreference: %d\n", mxRecord.Host,            mxRecord.Pref)   }}

Get Security with Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.