I would like to get a specific value from an environment variable in my Kotlin application, but I cannot find anything about reading environment variables in core libraries .
I expect it to be under kotlin.system , but there really isn’t much there.
You can use the kotlin Konfig extension
Konfig - Type of Secure Configuration API for Kotlin
Konfig , API , - , , , , ..
:
val envVar : String? = System.getenv("varname")
, , , Java System, Kotlin's.
It is very simple to get the environment value if it exists or the default value using the elvis operator in kotlin:
var envVar: String = System.getenv("varname") ?: "default_value"
My favorite airliner:
val myEnv = if (System.getenv("MY_ENV").isNullOrEmpty()) "default_value" else System.getenv("MY_ENV")