DNS查询A记录
首先需要安装go dns包
go get github.com/mikg/dns
type Msg struct {
MsgHdr
Compress bool `json:"-"` // If true, the message will be compressed when converted to wire format.
Question []Question // Holds the RR(s) of the question section.
Answer []RR // Holds the RR(s) of the answer section.
Ns []RR // Holds the RR(s) of the authority section.
Extra []RR // Holds the RR(s) of the additional section.
}
Exchange函数的返回值为Msg,结构体如上.
创建一个Msg,向DNS服务器发送需要查询的网址以及设置要查询的类型为A记录.获取Exchange返回的结构体,查询其中的Answer内容.
代码如下:
package main
import (
"fmt"
"github.com/miekg/dns"
)
func main() {
var msg dns.Msg //创建一个Msg
fqdn := dns.Fqdn("stacktitan.com") //调用fqdn将域转换为可以与DNS服务交换的FQDN
msg.SetQuestion(fqdn, dns.TypeA)