go程序中的dns解析方式优先级
Sun 12 October 2025
golang有两种域名解析方法:内置go解析器和基于cgo的系统解析器。
默认情况下用的是内置解析,如果你想指定使用cgo解析器,可以build的时候指定。
export GODEBUG=netdns=go # force pure Go resolver
export GODEBUG=netdns=cgo # force cgo resolver
const (
// hostLookupCgo means defer to cgo.
hostLookupCgo hostLookupOrder = iota
hostLookupFilesDNS // files first
hostLookupDNSFiles // dns first
hostLookupFiles // only files
hostLookupDNS // only DNS
)
var lookupOrderName = map[hostLookupOrder]string{
hostLookupCgo: "cgo",
hostLookupFilesDNS: "files,dns",
hostLookupDNSFiles: "dns,files",
hostLookupFiles: "files",
hostLookupDNS: "dns",
}
根据操作系统的不同,使用的解析策略也会略有不同
当 前 系 统 如 果 是 linux 并 且 不 存 在 /etc/nsswitch.conf 文 件 的 时 候 , 会 直 接 返 回 dnsfiles 的 顺 序 , 这 个 是 参 考 了 glibc 的 实 现
Category: 数字生活