Recently I am reading source code of net
package of Go. When I was diving into the name resolution part, it looked like Go had queried /etc/hosts
twice:
First in 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)
Then in 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
}
}
So is this a bug? Or It does have some reason but not documented?