I am trying to extract the nameservers from a DNS record using GoLang. The issue I'm walking into is that I'm unable to read the fields from a Struct.
I'm turning the Response into JSON so I can "read" which fields it has, I'm using the following code for that:
json, _ := json.Marshal(ns)
fmt.Println(string(json))
This prints out:
{"Hdr":{"Name":"example.com.","Rrtype":2,"Class":1,"Ttl":172800,"Rdlength":16},"Ns":"ns2.example.eu."}
Now when I try to read print out the Name value from this string using:
fmt.Println(ns.Hdr.Name)
I get the following error:
./main.go:19:18: ns.Hdr undefined (type dns.RR has no field or method Hdr)
Could anyone help me to extract the Name (example.com.) from the Ns (Struct?).
package main
import (
"encoding/json"
"fmt"
"github.com/lixiangzhong/dnsutil"
)
func main() {
var dig dnsutil.Dig
dig.SetDNS("8.8.4.4")
res, _ := dig.Trace("example.com.")
for _, nameservers := range res {
for _, ns := range nameservers.Msg.Ns {
json, _ := json.Marshal(ns)
fmt.Println(string(json))
fmt.Println(ns.Hdr.Name)
}
}
}
I expected this to print out "example.com." but instead I get an error.
Thanks!
to avoid compilation error change Hdr to Header()
package main
import (
"encoding/json"
"fmt"
"github.com/lixiangzhong/dnsutil"
)
func main() {
var dig dnsutil.Dig
dig.SetDNS("8.8.4.4")
res, _ := dig.Trace("example.com.")
for _, nameservers := range res {
for _, ns := range nameservers.Msg.Ns {
json, _ := json.Marshal(ns)
fmt.Println(string(json))
fmt.Println(ns.Header().Name)
}
}
}