net.LookupHost vs net.LookupIP

263 views
Skip to first unread message

Simon Menke

unread,
Nov 7, 2014, 4:55:28 AM11/7/14
to golan...@googlegroups.com
I'm wondering what the exact difference is between net.LookupHost and net.LookupIP

Jan Mercl

unread,
Nov 7, 2014, 5:17:16 AM11/7/14
to Simon Menke, golang-nuts
On Fri, Nov 7, 2014 at 10:55 AM, Simon Menke <simon...@gmail.com> wrote:
> I'm wondering what the exact difference is between net.LookupHost and
> net.LookupIP

The documentation of both are a one liners, but seems clear: One
translates a string to a list of numbers, the other translates a
number to a string. (Both use the DNS for the resolution.)

-j

Christian von Kietzell

unread,
Nov 7, 2014, 5:23:49 AM11/7/14
to Jan Mercl, Simon Menke, golang-nuts
Hi,

Both seem to translate a host to an IP address, but LookupHost() returns the addresses as a string slice while LookupIP() returns them as a net.IP slice (which in turn is []byte).


  Chris

Simon Menke

unread,
Nov 7, 2014, 5:29:18 AM11/7/14
to golan...@googlegroups.com, 0xj...@gmail.com, simon...@gmail.com
Sure, the return types are different. But, when reading through the code, this doesn't seem to be the only difference.
LookupIP seems to be merging its requests while LookupHost doesn't seem to be doing this.

Jan Mercl

unread,
Nov 7, 2014, 5:35:11 AM11/7/14
to Christian von Kietzell, Simon Menke, golang-nuts
On Fri, Nov 7, 2014 at 11:23 AM, Christian von Kietzell
<cub...@gmail.com> wrote:
> Both seem to translate a host to an IP address

Sorry, you're right. Same names, different package.

Seems the only diff is that the former asks known name servers
possibly in parallel while the later does not[0]. The RFC suggests the
parallel way and first answer wins, IIRC.

[0]: http://golang.org/src/pkg/net/lookup.go#L36

-j

Ian Taylor

unread,
Nov 7, 2014, 12:24:38 PM11/7/14
to Simon Menke, golang-nuts, Jan Mercl
On Fri, Nov 7, 2014 at 2:29 AM, Simon Menke <simon...@gmail.com> wrote:
>
> Sure, the return types are different. But, when reading through the code,
> this doesn't seem to be the only difference.
> LookupIP seems to be merging its requests while LookupHost doesn't seem to
> be doing this.

This is only a difference in implementation, not in effect. Both
functions look up a name in DNS and return IP addresses.

When LookupIP was introduced, it was slightly different in that
LookupHost looked in /etc/hosts and LookupIP did not. That is,
LookupHost first looked in /etc/hosts, and then, if it didn't find
anything, called LookupIP. So LookupIP only used DNS and LookupHost
check /etc/hosts and then fell back to DNS.

However, this distinction was only relevant when using the Go native
resolver, not when using getaddrinfo which is now the default.

Further, http://codereview.appspot.com/4431054 changed LookupIP to use
/etc/hosts.

So now there is basically no difference between LookupHost and
LookupIP. We should encourage people to use LookupIP.

Ian
Reply all
Reply to author
Forward
0 new messages