gpt4 book ai didi

go - 为什么golang Lookup*** 函数不能提供服务器参数?

转载 作者:IT老高 更新时间:2023-10-28 13:05:23 24 4
gpt4 key购买 nike

对于nslookup 命令,它有nslookupsomewhere.com some.dns.server

但是,golang dnsclient 似乎只从 /etc/resolv.conf

加载配置

此处代码:https://golang.org/src/net/dnsclient_unix.go#L225

golang 标准库是否提供类似func LookupTXT(name string, dnsServer string) (txt []string, err error) ?

要求:1.不要更改默认的/etc/resolv.conf

最佳答案

@holys

"github.com/miekg/dns is too heavy for me"

没那么重:

package main

import (
"log"

"github.com/miekg/dns"
)

func main() {

target := "microsoft.com"
server := "8.8.8.8"

c := dns.Client{}
m := dns.Msg{}
m.SetQuestion(target+".", dns.TypeA)
r, t, err := c.Exchange(&m, server+":53")
if err != nil {
log.Fatal(err)
}
log.Printf("Took %v", t)
if len(r.Answer) == 0 {
log.Fatal("No results")
}
for _, ans := range r.Answer {
Arecord := ans.(*dns.A)
log.Printf("%s", Arecord.A)
}
}

运行时,您应该看到:

$ go run dns.go
2015/07/26 00:24:46 Took 16.138928ms
2015/07/26 00:24:46 134.170.188.221
2015/07/26 00:24:46 134.170.185.46

关于go - 为什么golang Lookup*** 函数不能提供服务器参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30043248/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com