package main import ( "fmt" "github.com/miekg/dns" ) func main() { client := dns.Client{} msg := dns.Msg{} msg.SetQuestion("example.com.", dns.TypeA) response, _, err := client.Exchange(&msg, "8.8.8.8:53") if err != nil { fmt.Printf("Error: %s\n", err) return } for _, answer := range response.Answer { fmt.Printf("%v\n", answer) } }
package main import ( "fmt" "github.com/miekg/dns" ) func main() { client := dns.Client{} msg := dns.Msg{} msg.SetQuestion("google.com.", dns.TypeMX) response, _, err := client.Exchange(&msg, "8.8.8.8:53") if err != nil { fmt.Printf("Error: %s\n", err) return } for _, answer := range response.Answer { mx, ok := answer.(*dns.MX) if !ok { continue } fmt.Printf("%s: %d\n", mx.Mx, mx.Preference) } }This example sends a DNS query for the MX record(s) for google.com to the Google Public DNS server at 8.8.8.8:53. It then extracts the MX records from the response and prints out the mail exchange server(s) and their priorities.