Help us shape the future of Go! Take the 2026 Go Developer Survey

localhostdns

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 18, 2026 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package localhostdns provides a net.Resolver that resolves "localhost" itself instead of trusting the operating system to.

Go's pure resolver, used by any binary built with the netgo build tag (or run with GODEBUG=netdns=go), resolves "localhost" from the hosts file. Windows ships its hosts file with the localhost entries commented out ("localhost name resolution is handled within DNS itself"), expecting resolvers to special-case the name. Go's resolver does not (a fix is pending in https://go-review.googlesource.com/c/go/+/831864), so on Windows the query escapes to real DNS servers, which don't know the name, and dialing "localhost" fails. Official tailcat binaries no longer build with netgo, but packagers and GODEBUG can still select the pure resolver, so dialing through this resolver keeps localhost meaning loopback regardless. See https://github.com/tailscale/tailcat/issues/108.

Resolver answers "localhost" and any name ending in ".localhost" (the names RFC 6761 reserves to mean loopback) with both 127.0.0.1 and ::1, so a net.Dialer using it tries one loopback address and falls back to the other as usual for a dual-stack name. It answers every other DNS query with NXDOMAIN rather than forwarding to real DNS servers, making it usable only by dialers that dial loopback names, IP literals, and hosts-file names. That non-forwarding is also what makes it safe: a search domain from DHCP can expand "localhost" to a query like "localhost.lan" that a creative gateway might answer with a non-loopback address, and here that query gets NXDOMAIN like any other.

Index

Constants

This section is empty.

Variables

View Source
var Resolver = &net.Resolver{
	PreferGo: true,
	Dial:     dialFakeServer,
}

Resolver resolves localhost names to the loopback addresses without consulting real DNS servers, as described in the package comment. The hosts file still applies: Go's resolver consults it before asking any DNS server, including this fake one.

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL