Defining & Using Namespaces
Learn to manage code organization and prevent naming conflicts using Clojure's namespace system.
Why Namespaces Matter
Imagine building a large application with many files and functions. Without organization, you might accidentally use the same name for different things, leading to confusion and errors.
Namespaces in Clojure help you organize your code into logical groups, preventing these naming conflicts and making your projects easier to manage.
Declaring a Namespace
Every Clojure source file starts with a ns declaration. This macro defines the namespace for the code within that file. It usually matches the file's path.
For example, a file named src/my_app/core.clj would typically declare my-app.core as its namespace.
(ns my-app.core)
(defn greet [name]
(str "Hello, " name ". Welcome to " *ns* "))
(defn -main []
(println (greet "CoddyKit user")))All lessons in this course
- Understanding Clojure Macros
- Defining & Using Namespaces
- Organizing Code with Modules
- Protocols and Multimethods for Polymorphism