如何在Kotlin中读取环境变量? [英] How to read an environment variable in Kotlin?

查看:463
本文介绍了如何在Kotlin中读取环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的Kotlin应用程序中的环境变量中获取某个值,但是在

I'd like to get a certain value from an environment variable in my Kotlin app, but I can't find anything about reading environment variables in the core libraries documentation.

我希望它位于 kotlin.system ,但实际上没有那么多.

I'd expect it to be under kotlin.system but there's really not that much there.

推荐答案

使用kotlin中的elvis运算符很容易获得环境值(如果存在)或默认值:

It is really easy to get a environment value if it exists or a default value by using the elvis operator in kotlin:

var envVar: String = System.getenv("varname") ?: "default_value"

这篇关于如何在Kotlin中读取环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆