在Clojure中检测操作系统

jus*_*nhj 14 configuration clojure

*features在Clojure中是否有相当于Common Lisp的*,因此您可以检测操作系统和其他环境配置?或者我只是通过Java API?

Bri*_*per 18

可能使用Java API.这很容易,没有意义重新发明轮子.

user> (System/getProperty "os.name")
"Linux"
user> (System/getProperty "os.version")
"2.6.36-ARCH"
user> (System/getProperty "os.arch")
"amd64"
Run Code Online (Sandbox Code Playgroud)


sem*_*ros 5

要添加Brian Carper的答案,您可以通过Java API轻松创建系统属性的映射,并将其绑定到符号功能:

(def *features* {
  :name (System/getProperty "os.name"),
  :version (System/getProperty "os.version"),
  :arch (System/getProperty "os.arch")})
Run Code Online (Sandbox Code Playgroud)

这为您提供了这种结构,例如:

{:name "Windows 7", :version "6.1", :arch "x86"}
Run Code Online (Sandbox Code Playgroud)

然后通过以下任一方式访问属性:

(:name *features*)
(*features* :name)
(get *features* :name)
Run Code Online (Sandbox Code Playgroud)

无论哪个漂浮你的船.

  • `System.getProperties`返回一个Java Hashtable,所以你也可以做`(into {}(System/getProperties))`来获得字符串的Clojure映射到字符串并以这种方式拉出属性. (3认同)