Dane Balia
Dane Balia

Reputation: 5367

Elixir - Get Host By Name?

How do you gethostbyname with Elixir?

There doesn't seem to be a supported API and the two solutions seem to revolve around,

  1. Erlang's Inet
  2. Fork to shell with System (hostname)

Upvotes: 4

Views: 957

Answers (1)

The general philosophy in Elixir is that if a solution exists in standard erlang libraries, there is no reason to simply reproduce that solution with a elixir wrapper unless you are going to provide added functionality in some way.

Or in other words, erlang libraries are native.

iex(2)> :inet.gethostbyname('www.google.com')
{:ok, {:hostent, 'www.google.com', [], :inet, 4, [{216, 58, 192, 4}]}}

Note: the single quotes above are important, you can convert an Elixir string to an Erlang one by using String.to_charlist

iex(5)> :inet.gethostbyname(String.to_char_list("www.google.com"))
{:ok, {:hostent, 'www.google.com', [], :inet, 4, [{216, 58, 192, 4}]}}

Upvotes: 9

Related Questions