Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
net: listening on a local port for a test requires tedious boilerplate #24778
In the stdlib alone, there are 6 functions called
(there are admittedly differences in between them in how they handle errors, etc)
It would be nice (for consumers of the stdlib as well) if there was an easy way to do this with one line.
You might think that listening on the unspecified interface would help, but there are two downsides. First, you might not want your test server to be externally accessible! Second, if you listen on the unspecified interface then your listener.Addr() is like "0.0.0.0" or "::" which you may not be able to connect to in all environments (and you may run into issues like #24737 as well).
(Arguably this would go well in a net/nettest package like net/http/httptest, but that isn't a thing.)
Perhaps in the spirit of https://tools.ietf.org/html/draft-west-let-localhost-be-localhost-06 we should special case a lookup of "localhost" when the network is "tcp" (not "tcp4" or "tcp6") and just do the right thing, rather than adding any new API.
That is, if your system's DNS says that "localhost" is "127.0.0.1" and you're on an IPv6-only system, then Go shouldn't care about your DNS value if you say
Even without special casing, I believe "localhost" should do the right thing on any properly configured system. My
(My previous comment about "tcp" vs. "tcp6" is wrong; "tcp" happily resolves either v4 or v6 addresses.)