@@ -404,6 +404,41 @@ defmodule Application do
404
404
405
405
If the configuration parameter does not exist, the function returns the
406
406
`default` value.
407
+
408
+ ## Examples
409
+
410
+ `get_env/3` is commonly used to read the configuration of your OTP applications.
411
+ Since Mix configurations are commonly used to configure applications, we will use
412
+ this as a point of illustration.
413
+
414
+ Consider a new application `:my_app`. `:my_app` contains a database engine which
415
+ supports a pool of databases. The database engine needs to know the configuration for
416
+ each of those databases, and that configuration is supplied by key-value pairs in
417
+ environment of `:my_app`.
418
+
419
+ config :my_app, Databases.RepoOne,
420
+ # A database configuration
421
+ ip: "localhost"
422
+ port: 5433
423
+
424
+ config :my_app, Databases.RepoTwo,
425
+ # Another database configuration (for the same OTP app)
426
+ ip: "localhost"
427
+ port: 20717
428
+
429
+ config :my_app, my_app_databases: [Databases.RepoOne, Databases.RepoTwo]
430
+
431
+ Our database engine used by `:my_app` needs to know what databases exist, and
432
+ what the database configurations are. The database engine can make a call to
433
+ `get_env(:my_app, :my_app_databases)` to retrieve the list of databases (specified
434
+ by module names). Our database engine can then traverse each repository in the
435
+ list and then call `get_env(:my_app, Databases.RepoOne)` and so forth to retrieve
436
+ the configuration of each one.
437
+
438
+ **Important:** if you are writing a library to be used by other developers,
439
+ it is generally recommended to avoid the application environment, as the
440
+ application environment is effectively a global storage. For more information,
441
+ read our [library guidelines](/library-guidelines.html).
407
442
"""
408
443
@ spec get_env ( app , key , value ) :: value
409
444
def get_env ( app , key , default \\ nil ) do
0 commit comments