Creating UUIDs in Elixir

Viewed 14255

What's a canonical way to generate UUIDs in Elixir? Should I necessarily use the library https://hex.pm/packages/uuid or is there a built-in library? I better have less dependencies and do more work than vise versa, therefore if I can generate in Elixir with an external dependency, it'll better go with it.

4 Answers

The canonical way to generate a globally unique reference in Elixir is with make_ref/0.

From the documentation:

Returns an almost unique reference.

The returned reference will re-occur after approximately 2^82 calls; therefore it is unique enough for practical purposes.

If you don't want to include Ecto in your project, you should evaluate https://github.com/zyro/elixir-uuid

defp deps do
  [ { :elixir_uuid, "~> 1.2" } ]
end
iex> UUID.uuid1()
"5976423a-ee35-11e3-8569-14109ff1a304"
iex> UUID.uuid3(:dns, "my.domain.com")
"03bf0706-b7e9-33b8-aee5-c6142a816478"

iex> UUID.uuid3("5976423a-ee35-11e3-8569-14109ff1a304", "my.domain.com")
"0609d667-944c-3c2d-9d09-18af5c58c8fb"
Related