最近,我正在阅读Go的net
包的源代码。当我深入研究名称解析部分时,Go似乎两次询问/etc/hosts
:
net/dnsclient_unix.go
goLookupHostOrder中的第一名:
func (r *Resolver) goLookupHostOrder(ctx context.Context, name string, order hostLookupOrder) (addrs []string, err error) {
if order == hostLookupFilesDNS || order == hostLookupFiles {
// Use entries from /etc/hosts if they match.
addrs = lookupStaticHost(name)
if len(addrs) > 0 || order == hostLookupFiles {
return
}
}
ips, _, err := r.goLookupIPCNAMEOrder(ctx, name, order)
然后在goLookupIPCNAMEOrder中输入
:func (r *Resolver) goLookupIPCNAMEOrder(ctx context.Context, name string, order hostLookupOrder) (addrs []IPAddr, cname dnsmessage.Name, err error) {
if order == hostLookupFilesDNS || order == hostLookupFiles {
// goLookupIPFiles invokes lookupStaticHost internally.
addrs = goLookupIPFiles(name)
if len(addrs) > 0 || order == hostLookupFiles {
return addrs, dnsmessage.Name{}, nil
}
}
这是一个错误吗?还是有某些原因但没有记录?