我试图了解如何通过lein声明和要求命名空间。我创建了一个项目"采访",内部" src /采访"我有一个名为"提示"的文件夹,它有一个名为rawlist.clj的文件:
+ /interview project root
|--+ /src lein src
| |--+ /interview
| |--+ core.clj
| |--+ /prompts my new folder
| |--+ rawlist.clj
rawlist.clj文件:
(ns interview.prompts.rawlist)
;...
问题:
(require 'interview.prompts.rawlist)
答案 0 :(得分:3)
请参阅clojure docs for require。这是一个示例,其中命名空间被赋予别名r
,以便您可以缩短名称。
(require [interview.prompts.rawlist :as r])
(r/your-function)
您也可以随时参考全名:
(interview.prompts.rawlist/another-fn)
有许多方法可以使用require,例如如果你不想要前缀,请参考。假设您有3个函数f1,f2,f3,那么您可以通过以下方式引用它们:
(require [interview.prompts.rawlist :refer [f1 f2] :as r)
(f1)
(f2)
(r/f3)
请注意,在第三种情况下,由于它不在引用列表中,您必须使用r
前缀。
对于测试文件夹,请阅读解释它的documentation for leiningen,但基本归结为:
+ /interview
|--+ src/
|--+ test/
并且子文件夹结构遵循与src目录完全相同的模式。
您可以使用主项目宏中的project.clj
和:source-paths
键在:test-paths
中添加其他文件夹。有关详细信息,请参阅the sample project。
我建议你吸收上面示例项目中的所有内容,并阅读leiningen tutorial。
最后,当在其他来源(例如在您的测试中)引用ns
declaration中的函数时,您使用相同的格式,但使用:require
形式:
(ns foo.bar
(:require [interview.prompts.rawlist :as r]))
(r/foo-fn)