3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ElixirでOSの環境変数を利用する

Last updated at Posted at 2020-09-19

はじめに

System.get_env を使います

System.get_env/0

  • /のうしろの数字は引数の数です
  • 引数無しで呼び出すとすべての環境変数がマップで得られます
iex> System.get_env
%{
  "HELLO_NERVES_QIITA_READ_WRITE_TOKEN" => "secret",
  ...
}

System.get_env/1

  • 環境変数がセットされていない場合にはnilが返ります
iex> System.get_env("NHK_API_KEY")
"secret"

iex> System.get_env("NOT_SET")
nil

System.get_env/2

  • 環境変数がセットされていない場合のデフォルト値を第二引数で指定します
iex> System.get_env("NOT_SET")
nil

iex> System.get_env("NOT_SET", "4001")
"4001"

iex> System.get_env("NHK_API_KEY", "nhk")
"secret"

Wrapping Up

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?